Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript VueJS语法:在承诺中保存值_Javascript_Vue.js_Vuejs2 - Fatal编程技术网

Javascript VueJS语法:在承诺中保存值

Javascript VueJS语法:在承诺中保存值,javascript,vue.js,vuejs2,Javascript,Vue.js,Vuejs2,使用,当页面加载和单击按钮时,我可以运行我的loadData 但是,页面上的文本没有更新。我的this.text=xhr.data的语法有问题 index.html: <div id="app"></div> app.js: const Vue = window.Vue = require("vue"); Vue.prototype.$http = require("axios"); const App = require("./components/App.vue

使用,当页面加载和单击按钮时,我可以运行我的
loadData

但是,页面上的文本没有更新。我的
this.text=xhr.data的语法有问题

index.html:

<div id="app"></div>

app.js:

const Vue = window.Vue = require("vue");
Vue.prototype.$http = require("axios");
const App = require("./components/App.vue");

window.app = new Vue({
    el: "#app",
    render: h => h(App)
});

components/app.vue:

<template>
    <div>
        <h1>Test</h1>
        <p>{{text}}</p>
        <button @click="this.loadData">Reload</button>
    </div>
</template>
<script>
export default {
    mounted() {
        this.loadData();
    },
    methods: {
        loadData() {
            this.$http.get("https://icanhazip.com")
                // This fails
                .then(xhr => this.text = xhr.data);
        }
    }
};
</script>
index.html:
app.js:
常量Vue=window.Vue=require(“Vue”);
Vue.prototype.$http=require(“axios”);
const-App=require(“./components/App.vue”);
window.app=新的Vue({
el:“应用程序”,
渲染:h=>h(应用程序)
});
组件/app.vue:
试验
{{text}}

重新加载 导出默认值{ 安装的(){ 这是loadData(); }, 方法:{ loadData(){ 这是.$http.get(“https://icanhazip.com") //这失败了 .然后(xhr=>this.text=xhr.data); } } };
必须在组件数据中定义文本属性

从Vue.js文档:

由于现代JavaScript的限制(以及Object.observe的放弃),Vue无法检测属性的添加或删除。由于Vue在实例初始化期间执行getter/setter转换过程,因此数据对象中必须存在一个属性,以便Vue对其进行转换并使其响应。例如:

var vm = new Vue({
  data: {
    a: 1
  }
})
// `vm.a` is now reactive
vm.b = 2
// `vm.b` is NOT reactive
在您的情况下,您的组件应如下所示:

<script>
export default {
    created() {
        this.loadData();
    },
    data() {
        return {
            text: '',
        };
    },
    methods: {
        loadData() {
            this.$http.get("https://icanhazip.com")
                // This fails
                .then(xhr => this.text = xhr.data);
        }
    }
};
</script>

导出默认值{
创建(){
这是loadData();
},
数据(){
返回{
文本:“”,
};
},
方法:{
loadData(){
这是.$http.get(“https://icanhazip.com")
//这失败了
.然后(xhr=>this.text=xhr.data);
}
}
};

必须在组件数据中定义文本属性

从Vue.js文档:

由于现代JavaScript的限制(以及Object.observe的放弃),Vue无法检测属性的添加或删除。由于Vue在实例初始化期间执行getter/setter转换过程,因此数据对象中必须存在一个属性,以便Vue对其进行转换并使其响应。例如:

var vm = new Vue({
  data: {
    a: 1
  }
})
// `vm.a` is now reactive
vm.b = 2
// `vm.b` is NOT reactive
在您的情况下,您的组件应如下所示:

<script>
export default {
    created() {
        this.loadData();
    },
    data() {
        return {
            text: '',
        };
    },
    methods: {
        loadData() {
            this.$http.get("https://icanhazip.com")
                // This fails
                .then(xhr => this.text = xhr.data);
        }
    }
};
</script>

导出默认值{
创建(){
这是loadData();
},
数据(){
返回{
文本:“”,
};
},
方法:{
loadData(){
这是.$http.get(“https://icanhazip.com")
//这失败了
.然后(xhr=>this.text=xhr.data);
}
}
};

有错误消息吗?看起来您没有数据对象。只有数据对象中的属性是被动的。有错误消息吗?看起来您没有数据对象。只有数据对象中的属性是被动的。