vue.js变量

发布于 2025-01-30 14:25:47 字数 649 浏览 0 评论 0原文

我是vue.js(和HTML一般)的新手,我正在尝试使用vue.js自动化Bulma Tab Switch,而无需许多HTML文档。

<li :class="[ tabsel === 'profile' ? 'is-active' : '']"> <a @click="tabsel = 'profile'">My Profile</a></li>

这是要交换哪个行活动的行之一的示例。我很好奇可以在哪里初始化 tabsel 变量,范围如何工作?

最初,我有一个.js文件,然后加载了一个.js文件:

let app = {};

let init = (app) => {
    app.vue = new Vue({
        e1: "#vueapp",
        data () {
            return {
                tabsel: "profile",
            }
        }
    });
}

最终,我不确定第一个代码段是否应该能够看到此值。初始化此变量的正确方法是什么?在我的示例中,这不起作用,因为TABSEL是数据的成员,该数据不是明确的HTML位寻找的?

I am very new to Vue.js (and html in general), I am attempting to use Vue.js to automate Bulma tab switching without needing many HTML docs.

<li :class="[ tabsel === 'profile' ? 'is-active' : '']"> <a @click="tabsel = 'profile'">My Profile</a></li>

This is an example of one of the lines to swap which tab is active. I am curious where I can initialize the tabsel variable and how does scoping work?

I initially had a .js file that I loaded in as such:

let app = {};

let init = (app) => {
    app.vue = new Vue({
        e1: "#vueapp",
        data () {
            return {
                tabsel: "profile",
            }
        }
    });
}

Ultimately I am not sure if the first code snippet should be able to see this value. What is the proper way to initialize this variable? In my example does this not work because tabsel is a member of data which is not explicitly what the html bit is looking for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦行七里 2025-02-06 14:25:47

在您的示例中,带有e1的错误el,并且您忘记了创建HTML root Element(将用于安装应用程序)。

通过纠正这些错误,它可以在此处看到:

let app = {};

let init = (app) => {
    app.vue = new Vue({
        el: "#vueapp",
        data () {
            return {
                tabsel: "profile",
            }
        }
    });
}

init(app)
.is-active {
  color: red;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="vueapp">
  <ul>
    <li :class="[ tabsel === 'profile' ? 'is-active' : '']"> <a @click="tabsel = 'profile'">My Profile</a></li>
    <li :class="[ tabsel === 'example' ? 'is-active' : '']"> <a @click="tabsel = 'example'">Example</a></li>
  </ul>

</div>

至于范围,只要您的变量在data中定义,它们是反应性的,您可以在HTML模板中使用它们。

In your example, your mistyped el with e1 and you forgot to create the HTML root element (that will be used to mount the app).

By correcting these errors, it works as you can see here:

let app = {};

let init = (app) => {
    app.vue = new Vue({
        el: "#vueapp",
        data () {
            return {
                tabsel: "profile",
            }
        }
    });
}

init(app)
.is-active {
  color: red;
 }
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="vueapp">
  <ul>
    <li :class="[ tabsel === 'profile' ? 'is-active' : '']"> <a @click="tabsel = 'profile'">My Profile</a></li>
    <li :class="[ tabsel === 'example' ? 'is-active' : '']"> <a @click="tabsel = 'example'">Example</a></li>
  </ul>

</div>

As for the scope, as long as your variables are defined in data, they are reactive and you can use them in your HTML template.

吻泪 2025-02-06 14:25:47

在VUE JS中,它被称为状态。就您而言,“ Tabsel”是一个状态。该州将拥有将在应用程序中共享的数据。如果创建变量。您不能轻松地将值带到其他组件。这就是为什么引入国家的原因。如果使用VUEX,则可以在应用程序中全球访问VUEX中创建的状态。

In vue js, It is called as state. In your case, "tabsel" is a state. The state will be having the data which will be shared within the application. If you create a variable. You cannot take to values to other components easily. That's why states are introduced. If you use vuex, the state created in the vuex can be accessed globally inside the application.

感情洁癖 2025-02-06 14:25:47

在VUE中初始化此变量的适当方法是什么,您可以在Data()方法/对象中初始化变量,以及是否要对变量的值进行任何更改。您可以通过事件或安装/创建的钩子进行此操作。

根据您的代码

new Vue({
    el: '#app',
  data:{
    tabsel: null // Initializing
  },
  mounted() {
    this.tabsel = 'profile'; // Assinging value to the variable
  }
});
.is-active{
  background: #444;
  color: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-bind:class="{'is-active': tabsel === 'profile' }">
    My Profile
  </div>
</div>

What is the proper way to initialize this variable ?

In Vue, You can initialize the variable in data() method/object and if you want to make any changes in the value of the variable. You can do that via events or in mounted/created hooks.

Demo as per your code :

new Vue({
    el: '#app',
  data:{
    tabsel: null // Initializing
  },
  mounted() {
    this.tabsel = 'profile'; // Assinging value to the variable
  }
});
.is-active{
  background: #444;
  color: #eee;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <div v-bind:class="{'is-active': tabsel === 'profile' }">
    My Profile
  </div>
</div>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文