【发布时间】:2020-12-21 07:17:30
【问题描述】:
我想创建一个基于 ajax api 响应或数据的组件,其中包括:
- 模板
- 数据
- 方法 - 可能有多种方法
备注:响应或数据是动态的,不保存在文件中。
我尝试生成并返回结果,例如:
<script>
Vue.component('test-component14', {
template: '<div><input type="button" v-on:click="changeName" value="Click me 14" /><h1>{{msg}}</h1></div>',
data: function () {
return {
msg: "Test Componet 14 "
}
},
methods: {
changeName: function () {
this.msg = "mouse clicked 14";
},
}
});
</script>
然后编译上面的代码:
axios.get("/api/GetResult")
.then(response => {
comp1 = response.data;
const compiled = Vue.compile(comp1);
Vue.component('result-component', compiled);
})
.catch(error => console.log(error))
Vue.compile(comp1) 出现错误 -
- 模板应该只负责将状态映射到 UI。避免在模板中放置带有副作用的标签,例如
提前致谢
【问题讨论】:
-
/api/GetResult 返回了什么?
-
返回是代码的第一部分-
<script> Vue.component('test-component14', { ... </script>,这是一个完整的组件。
标签: vuejs2 vue-dynamic-components async-components