【问题标题】:Component v-if statement with properties in Nuxt / Vue (Typescript)在 Nuxt / Vue (Typescript) 中具有属性的组件 v-if 语句
【发布时间】:2018-07-28 03:11:40
【问题描述】:

我有一个想要基于属性有条件地渲染的组件:

<template>
<div class="logoContainer" v-if="showLogo">
  LOGO
</div>
</template>

<script lang="ts">
import Vue from 'vue'
import { Prop } from 'vue-property-decorator'

export default class extends Vue {
  @Prop({default: true})
  showLogo: boolean
}
</script>

这会产生错误:Property or method "showLogo" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

设置和引用我的showLogo 属性的最简单方法是什么?

【问题讨论】:

    标签: typescript vue.js vuejs2 vue-component nuxt.js


    【解决方案1】:

    我从未使用过vue-property-decorator,但根据文档,您遗漏了一些东西。

    <template>
    <div class="logoContainer" v-if="showLogo">
      LOGO
    </div>
    </template>
    
    <script lang="ts">
    import { Component, Vue, Prop } from 'vue-property-decorator'
    
    @Component
    export class MyComponent extends Vue {
    @Prop({default: true})
    showLogo: boolean
    }
    

    【讨论】:

    • 谢谢!那成功了。我正在导入 Vue 并导出组件,但缺少关键部分——@Component。有趣的是,除了推荐阅读 Declaring-Reactive-Properties 文档之外,它运行时不会出错。
    猜你喜欢
    • 2019-03-01
    • 2022-01-23
    • 1970-01-01
    • 2013-06-01
    • 2018-11-18
    • 2015-12-18
    • 2017-04-16
    • 2020-10-19
    • 1970-01-01
    相关资源
    最近更新 更多