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

编程入门 行业动态 更新时间:2024-10-15 02:28:40
本文介绍了如何扩展从 npm 包导入的 vue 组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

node_modules/vendor/somecomponent.vue

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

更新:

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

我正在使用 https://github/eliep/vue-avatar 和我需要用一个道具来扩展它,也许还要修改一下模板.

从'vue-avatar'导入{Avatar}Avatar = Vueponent('Avatar').extend({道具:['国籍']});导出默认{成分: {头像},...}

导致 TypeError: 无法读取未定义的属性 'extend'

解决方案

它似乎没有具体记录,但是 Vue 本身的 extend 方法 可用于组件.您可以覆盖模板并添加方法(以及数据和道具).

Vueponent('someComponent', {模板:'<div>hi {{name}} {{one}}</div>',数据:() =>({名称:'原始'}),道具:['一个'],方法: {原文:函数(){this.name = '原始';}}});const myC = Vueponent('someComponent').extend({模板:'#我的模板',道具:['二'],方法: {另一个:函数(){this.name = '另一个';}}});新的 Vue({el: '#app',成分: {我的组件:myC}});

<script src="//cdnjs.cloudflare/ajax/libs/vue/2.2.3/vue.min.js"></script><div id='应用程序'><my-component one="arf" two="meow"></my-component>

<模板 id="我的模板"><div>看,它是 {{name}} {{one}} {{two}}<button @click="original">调用原件</button><button @click="another">呼叫另一个</button>

</template>

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

Vueponent('originalAvatar', Avatar);const newAvatar = Vueponent('originalAvatar').extend({/* 你的规格 */});

If you have a vue component that is installed via Node :

node_modules/vendor/somecomponent.vue

Is there any way to modify/extend the template/methods of this component?

Update:

After trying out the example below, I'm facing this issue:

I am using https://github/eliep/vue-avatar and I need to extend this with a prop, and maybe amend the template a bit.

import {Avatar} from 'vue-avatar'

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

export default {
        components: {
            Avatar
        }, 

       ...
}

Results in TypeError: Cannot read property 'extend' of undefined

解决方案

It doesn't appear to be documented specifically, but the extend method of Vue itself is available to components. You can override the template and add methods (and data and props).

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

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


new Vue({
  el: '#app',
  components: {
    myComponent: myC
  }
});

<script src="//cdnjs.cloudflare/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 appears to be a component spec, the simple JavaScript object that Vue will create a Component object from. Try this:

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

这篇关于如何扩展从 npm 包导入的 vue 组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-27 01:32:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145590.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组件   npm   vue

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!