简体   繁体   English

Vue 2 - 在组件中使用组件

[英]Vue 2 - using components inside components

I have the following code: import './menu-item';我有以下代码: import './menu-item'; import ItemImage from "./item-image";从“./item-image”导入ItemImage;

Vue.component('quest-card', {
    props: {
        title: String,
        isFree: Boolean,
        points: Number,
        item: String,
        level: Number,
    },
    components: {
        ItemImage
    },
    methods: {
        showFree: function() {
            return !!+this.isFree;
        },
    },
    template: `
      <section class="quest-reward bg-dark">
      <div class="rewardLevel">{{ level }}</div>
      <div v-if="!showFree()" class="bg-success freeReward">FREE</div>
      <div class="rewardItem">
        <item-image name="item" heb-name="test" rarity="epic"></item-image>
      </div>
      </section>
    `,
})

My component are used in a legacy HTML/JQuery website using selector (for this one - <quest-card...> and it's not rendered with the item-image component:我的组件在使用选择器的旧版 HTML/JQuery 网站中使用(对于这个 - <quest-card...> 并且它不使用 item-image 组件呈现:

在此处输入图像描述

Item image component looks like this:项目图像组件如下所示:

const ItemImage = Vue.component('ItemImage', {
    props: ['name','heb-name', {
        rarity: {
            default: 'normal',
            type: String,
        }
    }],
    computed: {
      glowClass: () => {
          return `glow-${this.rarity}`;
        }
    },
    template: `
      <img v-bind:src="'images/items/' + item + '.png'" v-bind:class="glowClass"/>
    `,
})

export default ItemImage;

the export default of the Vue component seem wrong, but not sure what I have to do so they can play together. Vue 组件的导出默认值似乎是错误的,但不确定我必须做什么才能让它们一起玩。 Any idea?任何想法?

You had problems in defining the props你在定义道具时遇到了问题

 const ItemImage = Vue.component('ItemImage', { props: {name: String, 'heb-name': String, item: [String, Number], rarity: { default: 'normal', type: String, }}, computed: { glowClass: () => { return `glow-${this.rarity}`; } }, template: ` <img v-bind:src="'images/items/' + item + '.png'" v-bind:class="glowClass"/> `, }) Vue.component('quest-card', { props: { title: String, isFree: Boolean, points: Number, item: String, level: Number, }, components: { ItemImage }, methods: { showFree: function() { return.;+this,isFree, }: }, template: ` <section class="quest-reward bg-dark"> <div class="rewardLevel">{{ level }}</div> <div v-if=",showFree()" class="bg-success freeReward">FREE</div> <div class="rewardItem"> <item-image name="item" heb-name="test" rarity="epic"></item-image> </div> </section> `, }) new Vue({ el: '#app', })
 <div id="app"><quest-card /></div> <script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM