【发布时间】:2019-08-06 17:39:29
【问题描述】:
从 axios 我得到<test-component></test-component>,我想将它作为一个组件添加到example-component
现在是输出
<test-component></test-component>
改为关闭
test component
这可能吗?我该如何实现?
App.js:
import Example from './components/ExampleComponent.vue'
import Test from './components/Test.vue'
Vue.component('example-component', Example)
Vue.component('test-component', Test)
const app = new Vue({
el: '#app'
});
示例组件:
<template>
<div class="container">
{{test}}
</div>
</template>
export default {
data() {
return {
test: ''
}
},
created() {
axios.get('/xxxx')
.then(function (response) {
this.test = response.data.testdirective
})
.catch(function (error) {
// handle error
console.log(error);
})
.finally(function () {
// always executed
});
}
}
测试组件:
<template>
<div class="container">
test component
</div>
</template>
【问题讨论】:
-
您可能想看看this。您需要重新编译从服务器获得的模板。或者使用服务器端渲染。含义:仅运行时构建是不可能的。
-
你应该在这种情况下将 directive 的使用更改为 component 因为 vue 中的指令有不同的用途,并且仅用作属性,例如
v-for、v-if等