有没有办法让一个.vue文件负责在单个文件组件模式中创建自己的Vue实例?

这是Vue文件.

// MyComponent.vue
<template><div>Hello {{ name }}!</div></template>

<script>
const Vue = require('vue');

// what would usually be exports default
const componentConfig = {
    name: "my-component",
    props: {
        name: String,
    },
};

function create(el, props) {
    const vm = new Vue({
        el,
        render(h) {
            return h(componentConfig, { props });
        });
    vm.$mount();
    return vm;
}

module.exports = { create };
</script>

然后是一些JS文件中的用法:

// index.js
const MyComponent = require('./MyComponent.vue');

const el = '.container';
const props = {
    name: 'Jess',
};

MyComponent.create(el, props);
</script>

当我执行上述操作时,会出现无法找到模板的错误.

[Vue warn]: Failed to mount component: template or render function not defined.

found in

---> <MyComponent>
       <Root>

出于本能,我不明白Vue编译器如何(从脚本标记中)神奇地推断出I want引用了上面声明的模板...所以是 啊有没有解释为什么我不能这样做,或者思考如何让它工作?

推荐答案

您所描述的内容是在Webpack和Vue Loader的预编译步骤中完成的.Vue编译器实际上并不解析Single File Components.Vue编译器can解析的是组件的选项对象中提供的模板.所以,如果你在componentConfig对象中提供template选项,你的例子就行了.否则,您必须使用Webpack和Vue Loader完成预编译步骤,才能解析单个文件组件的模板.要做到这一点,你必须遵守证监会的 struct .这里有一段摘录..

Template

  • 每个*.vue文件一次最多只能包含一个<template>
  • 内容将被提取并传递到vue-template-compilervue-template-compiler

Script

  • *.vue个文件一次最多只能包含一个块.
  • 该脚本作为ES模块执行.
  • The 102 should be a Vue.js 100. Exporting an extended constructor created by 101 is also supported, but a plain object is preferred.
  • .js个文件(或扩展名)匹配的任何网页包规则

为了使你的具体例子有效,你可以像这样重写main.js个文件..

const MyComponent = require("./MyComponent.vue");

const el = ".container";
const data = {
  name: "Jess"
};

MyComponent.create(el, data);

还有你的MyComponent.vue文件(这就像下面提到的@Ferrybig一样是js文件)..

<script>
const Vue = require('vue');

function create(el, data) {
  const componentConfig = {
    el,
    name: "my-component",
    data,
    template: `<div>Hello {{ name }}!</div>`
  };
  const vm = new Vue(componentConfig);
  return vm;
}

module.exports = { create };
</script>

看到这个了吗

或者,如果你更喜欢渲染函数,你的MyComponent.vue会是这样的..

<script>
const Vue = require('vue');

function create(el, data) {
  const componentConfig = {
    el,
    name: "my-component",
    data,
    render(h) { return h('div', 'Hello ' + data.name) } 
  };
  const vm = new Vue(componentConfig);
  return vm;
}

module.exports = { create };
</script>

CodeSandbox


One last thing to keep in mind:在任何组件中,您都可以使用模板或渲染功能,but not both就像您在示例中所做的那样.这是因为其中一个将覆盖另一个.例如,请参见JSFiddle Vue boilerplate,并注意模板是如何被覆盖的.这可以解释你所犯的错误.渲染函数优先,但您为其提供了组件的选项对象,该对象不提供要渲染的模板.

Vue.js相关问答推荐

具有VUE身份验证的Azure AD

如何将$validator实例注入到使用组合API语法的Vue2.7应用程序中

Pinia+Vue 3状态react 性-StoreToRef的替代方案

无法在cPanel/SSH上构建 | Laravel + Vue优化

为什么 Vue 在将设置脚本中的计算(computed)属性渲染到模板时找不到变量

在 Vuejs 中更新 Chartjs 图表数据集

vuejs3 youtube-plugin YT 未定义控制台错误

VeeValidate 4 字段验证状态

使用 nuxt.js 组件自动导入对性能不利吗?

在 Vuex 的状态下动态创建一个响应式数组

如何使用 Vue 命名插槽呈现静态内容列表?

带有外部配置文件的 Vue js

使用 vue.js 的 axios 预检失败错误 301

在 vuejs 中的图像中包含 router-link 标记

全局方法和实例方法有什么区别?

更新 v-for 中使用的数组内的对象时,Vue js react性问题

将自定义部分添加到 v-autocomplete 下拉列表

Vue.js:从 parent父Vue 获取数据

Vue3在按钮单击时以编程方式创建组件实例

在 Vue.js 模板中调用函数