【问题标题】:Vue Js Mounted did not triggred onpage load页面加载时未触发 Vue Js Mounted
【发布时间】:2020-07-29 07:58:59
【问题描述】:

不确定为什么mounted没有触发其中的任何功能。页面加载时显示'ss'测试也无法打印出来。

export default {
  name: 'loads',
  components: {
    Overlay,
  },
  data() {
    return {
      subscribed: true,
    };
  },
computed: {
    filteredLocation() {
      return this.$store.getters['location/filterLocation'](this.searchTerm);
    },
  },
  methods: {
    checkSubscriptionValidity() {
      this.$store.dispatch('user/checkTrial').then((res) => {
        this.subscribed = res.data;
      });
    },
  },
  mounted() {
    console.log('ss');
    this.checkSubscriptionValidity();
  },
};

还有其他原因会导致此挂载未触发问题吗?

【问题讨论】:

  • 没有错误?你能给我们看更多的代码吗
  • @Ifaruki 是的,没有错误。但未触发已安装。我已经编辑了代码并添加了整个脚本
  • 我猜这是一个组件。那么你在哪里导入组件呢?
  • 尝试 created() 钩子。如果它没有被调用,那么看看你在哪里渲染这个组件。
  • @Ifaruki,它是从 App.vue 路由的父组件。其他 Vue 文件路由在mounted() 中运行良好。只是这个文件挂载的钩子没有被触发

标签: javascript vue.js vuejs2


【解决方案1】:

请试试这个语法:

  computed: {
    filteredLocation: () => {
      return this.$store.getters['location/filterLocation'](this.searchTerm);
    },
  },
  methods: {
    checkSubscriptionValidity: () => {
      this.$store.dispatch('user/checkTrial').then((res) => {
        this.subscribed = res.data;
      });
    },
  },
  mounted: () => {
    console.log('ss');
    this.checkSubscriptionValidity();
  },

【讨论】:

  • 很抱歉,它不受挂载钩子的影响。使用function()方法也一样
  • 请看我附上的图片:D
  • 仍然没有触发/调用挂载的钩子
  • 你再试一次吗?我更新了以下代码,请仔细查看我附上的图片。 :D
  • 是的,我试过了,但我知道这段代码与地雷相同。我已经通过更改文件命名解决了这个问题。我不确定是否是因为提前命名 location.vue 导致了这个问题。但确定我可以将其标记为已接受的答案以供其他人参考
猜你喜欢
  • 2018-10-07
  • 1970-01-01
  • 2019-03-03
  • 1970-01-01
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 2020-04-21
  • 2017-07-05
相关资源
最近更新 更多