Vue.js组件注册模板



这里有一个非常简单的组件

const MyComponent = Vue.component('my-component', {
data () {
// data here...
},
methods: {
// methods here...
},
template: '<p>Hello, world !!!</p>'
});

是否可以使用外部文件来编写html代码<p>Hello, world!</p>而不是字符串模板?

我知道这是一个方法与单文件组件(https://v3.vuejs.org/guide/single-file-component.html)。

但不幸的是,我不能这样做,因为在我的上下文中,我不能使用像Vue CLI这样的工具,例如

示例中的template值可以从任何地方获取。所以可以这样做:

template.js:

export default {
return "<p>Hello, world!!!</p>"
}

component.js

import template from 'template'
const MyComponent = Vue.component('my-component', {
template: template,
});

最新更新