我想创建一个基于 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。避免在模板中放置带有副作用的标签,例如 <script>,因为它们不会被解析。
提前致谢