简体   繁体   English

VueJS + TS; 组件属性未绑定

[英]VueJS + TS; Component property not binding

I am trying to bind a component property by setting the related component attribute to a value but it is not binding the value when inspecting with Vue devtools or when outputting the value into the HTML. 我试图通过将相关的组件属性设置为值来绑定组件属性,但是在使用Vue devtools进行检查或将值输出到HTML中时,它没有绑定该值。 The value remains to be set to the default value that is set on the component. 该值将保留设置为组件上设置的默认值。

I event set a string attribute to just a static string and even that is not binding. 我将字符串属性设置为仅是静态字符串,甚至没有绑定。

The component also isn't outputted into the html at all, besides the top level div, but the Vue devtools do detect the component in the dom. 除了顶级div之外,该组件也根本没有输出到html中,但是Vue devtools确实会检测dom中的组件。

Code: 码:

Component HTML: 组件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>

Component ts: 组件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);
  }
}

Parent HTML using the component: 使用该组件的父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" />

Parent ts: 家长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");
    }
}

So, turned out my error was quite a simple one but easy to overlook. 因此,事实证明我的错误很简单,但是很容易忽略。

I had forgotten to put the '@Component' decorator on my 'NopeGamingView' which caused it to not be an actual component. 我忘记将“ @Component”装饰器放在“ NopeGamingView”上,这导致它不是实际的组件。 If you encounter as similar problem make sure you have the decorator on your view. 如果遇到类似问题,请确保您的视图上有装饰器。

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

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