Vue变量中以$或者_开头的变量无法访问的问题
今天做项目的时候,使用v-if遇到了一个报错,说的是变量不能用$或者_开头
Property “_obj” must be accessed with “$ data.obj" because properties starting with “$” or "” are not proxied in the Vue instance to prevent conflicts with Vue internalsSee
查了一下官网,官网在文档里面说的很清楚:
以 _ 或 $ 开头的 property 不会被 Vue 实例代理,因为它们可能和 Vue 内置的 property、API 方法冲突。你可以使用例如 vm.$data._property 的方式访问这些 property。
所以使用_$开头的变量,需要用 $data.xxx
xxx是变量名
在结构里面和方法里面都需要
<div id="app">
<div v-if="$data._num == 1"><div>
<button @click="click"></button>
<div>
<script>
new Vue({
e:'#app',
data:{
_num:0
},
methods:{
click(){
this.$data._num = 1
}
}
})
</script>