gpt4 book ai didi

vue.js - 如何扩展从 npm 包导入的 vue 组件?

转载 作者:搜寻专家 更新时间:2023-10-30 22:22:10 26 4
gpt4 key购买 nike

如果你有一个通过 Node 安装的 vue 组件:

node_modules/vendor/somecomponent.vue

有没有办法修改/扩展这个组件的模板/方法?

更新:

尝试下面的例子后,我遇到了这个问题:

我正在使用 https://github.com/eliep/vue-avatar我需要用一个 Prop 来扩展它,也许还要稍微修改一下模板。

import {Avatar} from 'vue-avatar'

Avatar = Vue.component('Avatar').extend({
props: ['nationality']
});

export default {
components: {
Avatar
},

...
}

导致 TypeError: Cannot read property 'extend' of undefined

最佳答案

似乎没有具体记录,但是 the extend method of Vue itself可用于组件。您可以覆盖模板并添加方法(以及数据和 Prop )。

Vue.component('someComponent', {
template: '<div>hi {{name}} {{one}}</div>',
data: () => ({
name: 'original'
}),
props: ['one'],
methods: {
original: function() {
this.name = 'original';
}
}
});

const myC = Vue.component('someComponent').extend({
template: '#my-template',
props: ['two'],
methods: {
another: function() {
this.name = 'another';
}
}
});


new Vue({
el: '#app',
components: {
myComponent: myC
}
});
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.2.3/vue.min.js"></script>
<div id='app'>
<my-component one="arf" two="meow"></my-component>
</div>

<template id="my-template">
<div>Look, it's {{name}} {{one}} {{two}}
<button @click="original">Call original</button>
<button @click="another">Call another</button>
</div>
</template>

Avatar 似乎是一个组件规范,Vue 将从中创建组件对象的简单 JavaScript 对象。试试这个:

Vue.component('originalAvatar', Avatar);
const newAvatar = Vue.component('originalAvatar').extend({
/* Your spec */
});

关于vue.js - 如何扩展从 npm 包导入的 vue 组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42766697/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com