VueJS + TS;组件属性未绑定

时间:2019-03-08 17:35:33

标签: javascript typescript vue.js vue-component

我试图通过将相关的组件属性设置为值来绑定组件属性,但是在使用Vue devtools进行检查或将值输出到HTML中时,它没有绑定该值。该值仍需设置为组件上设置的默认值。

我将字符串属性设置为仅是静态字符串,甚至没有绑定。

除了顶级div之外,该组件也根本没有输出到html中,但是Vue devtools确实可以检测到dom中的组件。

代码:

组件HTML:

<style scoped lang="sass">
  @import './discord-widget.scss';
</style>

<template>
  <div>
    <b-card bg-variant="dark" :header="`Currently online: ${widgetData.members.length}`" text-variant="white">
      <div v-for="user in widgetdata.members" class="discord-member">
        <img :src="user.avatar_url" alt="" class="d-inline-block">
        <div class="d-inline-block align-top has-game" v-if="user.game">
          <span>{{ user.username }}#{{user.discriminator}}</span>
          <span><br />Playing <b>{{ user.game.name }}</b></span>
        </div>
        <div class="d-inline-block" v-else>
          <span>{{ user.username }}#{{user.discriminator}}</span>
        </div>
      </div>
    </b-card>
  </div>
</template>

<script src="./discord-widget.ts"></script>

组件ts:

import Vue from "vue";
import { DiscordWidgetResult } from "../../models/discord";
import Component from "vue-class-component";
import { Prop } from "vue-property-decorator";

@Component
export default class DiscordWidgetComponent extends Vue {

  @Prop(Object) public widgetdata: DiscordWidgetResult = {} as DiscordWidgetResult;
  @Prop(String) public test: string = "";

  async mounted() {
    this.widgetdata.members = this.widgetdata.members.sort((a, b) => a.game ? -1 : b.game ? -1 : 0);
  }
}

使用该组件父HTML:

<discord-widget :widgetdata="widgetdata" v-on:load="getWidgetData" :test="'test'" class="pull-right ml-auto p-2 d-none d-sm-none d-md-none d-lg-block sticky-top" />

父母ts:

import Vue from "vue";
import { Provide } from "vue-property-decorator";
import { DiscordWidgetResult } from "../../models/discord";
import { discordWidgetService } from "../../boot";

export default class NopeGamingView extends Vue {

    @Provide()
    public widgetdata: DiscordWidgetResult = {} as DiscordWidgetResult;

    async created() {
    }

    async getWidgetData() {
        this.widgetdata = await discordWidgetService.GetGuildData();
        console.log("get data");
    }
}

1 个答案:

答案 0 :(得分:0)

所以,事实证明我的错误很简单,但是很容易忽略。

我忘记将“ @Component”装饰器放在“ NopeGamingView”上,这导致它不是实际的组件。如果遇到类似问题,请确保视图上有装饰器。