【问题标题】:Vue loses prototypes and imported files when creating new componentVue 在创建新组件时丢失原型和导入的文件
【发布时间】:2019-03-19 09:09:32
【问题描述】:

将 Vue 组件扩展为 modal 时,会生成一个新的 Vue 实例。这会导致丢失所有预先导入的助手/Vuex-stores 等。

有没有办法将所有数据复制到新扩展的 Vue 组件中?

为了清楚起见,看看这个jsfiddle

它会向您发出警报。当您单击按钮时,将打开模式。但是,如果您查看我的代码,应该会触发另一个警报。但是 $hi() 在其他 Vue 实例中不可用。

我创建了一个这样的组件:

const createComponent = (props) => {
    const wrapper = document.createElement('div');

    const Component = Vue.extend(MyComponent);

    return new Component({
        propsData: {
            props,
        },
    }).$mount(wrapper);
};

我使用SweetAlert 作为模态。

所以我添加如下:

swal({
    content: createComponent(props).$el,
})

【问题讨论】:

    标签: vue.js


    【解决方案1】:

    如果你移动这条线

    Vue.prototype.$hi = () => alert('hi');
    

    上面

    let component = new TestComponent().$mount(wrapper)
    

    它会起作用的。您的第一个组件在定义之前引用了$hi()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-12
      • 2021-03-25
      • 2021-01-04
      • 2018-11-10
      • 2021-05-24
      • 2017-06-19
      • 1970-01-01
      • 2019-04-27
      相关资源
      最近更新 更多