首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我使用“vue-property-decorator”时,为什么代码不能工作?(Vue.js+TypeScript)

当我使用“vue-property-decorator”时,为什么代码不能工作?(Vue.js+TypeScript)
EN

Stack Overflow用户
提问于 2020-07-08 12:29:30
回答 1查看 715关注 0票数 0

我使用的是Vue.js + TypeScript,但我不明白为什么它不能与‘vue-属性装饰器’一起工作。这两条代码的目的是做同样的事情。你能帮我指出我哪里做错了吗?

此代码工作

代码语言:javascript
复制
<script lang="ts">
import Vue from "vue";
import axios from "axios";
export default Vue.extend({
  name: "DetailPage",
  props: ["id"],
  data() {
    return {
      profile: [{}] as Array<object>
    };
  },
  methods: {
    findProfile(id?: string) {
      this.profile = [];
      axios
        .get("https://api.unsplash.com/photos/" + this.id, {
          headers: {
            Authorization: `Client-ID ${process.env.VUE_APP_MYVUE}`
          }
        })
        .then(res => {
          this.profile = res.data;
        })
        .catch(() => {
          this.profile = [];
        });
    }
  },
  watch: {
    $route(to: string, from: string) {
      this.findProfile();
    }
  },
  beforeMount() {
    this.findProfile();
  }
});
</script>

但是这个带有“vue-property-decorator”不工作

代码语言:javascript
复制
<script lang="ts">
import { Component, Vue, Prop, Watch } from "vue-property-decorator";
import axios from "axios";

export default class DetailPage extends Vue {
  @Prop() readonly id!: string;
  private profile: Array<object> = [];
  findProfile(id?: string) {
    const profile: Array<object> = []
    axios
      .get("https://api.unsplash.com/photos/"+ this.id , {
        headers: {
          Authorization: `Client-ID ${process.env.VUE_APP_MYVUE}`
        }
      })
      .then(res => {
        console.log(res.data);
      });
      
  }
  
  @Watch("$route", { immediate: true, deep: true })
  onUrlChange(to: any, from: any) {
    this.findProfile();
  }
  beforeMount() {
    this.findProfile();
  }
}
</script>

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2020-07-08 12:40:38

@Component装饰符指示该类是Vue组件

代码语言:javascript
复制
<script lang="ts">
import { Component, Vue, Prop, Watch } from "vue-property-decorator";
import axios from "axios";

 @Component({
        name: 'ComponentName',
        components: {example, exampleTwo}
    })

export default class DetailPage extends Vue {
  @Prop() readonly id!: string;
  private profile: Array<object> = [];
  findProfile(id?: string) {
    const profile: Array<object> = []
    axios
      .get("https://api.unsplash.com/photos/"+ this.id , {
        headers: {
          Authorization: `Client-ID ${process.env.VUE_APP_MYVUE}`
        }
      })
      .then(res => {
        console.log(res.data);
      });
      
  }
  
  @Watch("$route", { immediate: true, deep: true })
  onUrlChange(to: any, from: any) {
    this.findProfile();
  }
  beforeMount() {
    this.findProfile();
  }
}
</script>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62794852

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档