Vue.js 获取页面的当前标题并从嵌套组件进行更新,并在Vuejs 2中进行更新

Vue.js 获取页面的当前标题并从嵌套组件进行更新,并在Vuejs 2中进行更新,vue.js,vuejs2,Vue.js,Vuejs2,我有一个名为notifications.vue的组件,该组件被导入并在同样使用路由器视图的父组件中使用 非常基本的示例-my App.vue类似于: <Template> <Notifications></Notifications> <router-view></router-view> </Template> 我想对Notifications.vue执行的操作是,每当收到新通知时,获取浏览器选项卡的当前标题,并在其前面

我有一个名为notifications.vue的组件,该组件被导入并在同样使用路由器视图的父组件中使用

非常基本的示例-my App.vue类似于:

<Template>
<Notifications></Notifications>
<router-view></router-view>
</Template>

我想对Notifications.vue执行的操作是,每当收到新通知时,获取浏览器选项卡的当前标题,并在其前面添加1或任何数字。我尝试使用常规Document.title获取当前标题,但始终返回未定义的标题。还有什么方法可以做到这一点呢?

我假设您的通知组件中有一个数据对象

Notification.vue的简化版本

new Vue({
  data: {
    notifications: []
  },
  watch: {
    notifications (current, previous) {
      document.title = '(' + current.length + ')' + document.title.replace(/ *\([^)]*\) */g, "");
    }
  }
})
我们在这里做的是观察notifications对象的变化。如果它发生变化,我们将在文件标题前预先设置通知编号

document.title.replace/*\[^]*\*/g,此部分将删除当前通知计数,然后再使用新计数进行更新

这种方法的局限性:

如果标题中的括号中还有其他单词,它们将被删除


如果通知计数为零,则显示0标题;如果计数为1234,则显示1234标题。您可能需要进行更多的检查,以不显示零,如果长度大于9,则可能执行9+,我假设您的通知组件中有一个数据对象

Notification.vue的简化版本

new Vue({
  data: {
    notifications: []
  },
  watch: {
    notifications (current, previous) {
      document.title = '(' + current.length + ')' + document.title.replace(/ *\([^)]*\) */g, "");
    }
  }
})
我们在这里做的是观察notifications对象的变化。如果它发生变化,我们将在文件标题前预先设置通知编号

document.title.replace/*\[^]*\*/g,此部分将删除当前通知计数,然后再使用新计数进行更新

这种方法的局限性:

如果标题中的括号中还有其他单词,它们将被删除


如果通知计数为零,则显示0标题;如果计数为1234,则显示1234标题。您可能希望进行更多检查以不显示零,如果长度大于9,则可能执行9+,另一种方法是使用Vuex来管理状态

const store = new Vuex.Store({
  state: {
    notifications: []
  },
  mutations: {
    load (state, notifications) {
      state.notifications = notifications
    }
  },
  actions: {
    load (context) {
      Vue.$http.get('/notifications').then(response = {
        context.commit('load', response.data);
      })
    }
  }
});

// Notifications.vue
new Vue({
  mounted () {
    // you will want to add more than just an interval. You will want to keep track of this and perhaps stop it if, for example, the user logs out.
    setInterval(function () {
      store.dispatch('load');
    }.bind(this), 1000)
  }
});


// Add to your router
metaInfo: function () {
  return {
    title: '(' + store.state.notifications + ')' + 'Leads - ' + this.view
  }
}

这是一个使用Vuex解决此问题的快速示例。这不是测试,只是出于教育目的。更多信息请访问

另一种方法是使用Vuex管理状态

const store = new Vuex.Store({
  state: {
    notifications: []
  },
  mutations: {
    load (state, notifications) {
      state.notifications = notifications
    }
  },
  actions: {
    load (context) {
      Vue.$http.get('/notifications').then(response = {
        context.commit('load', response.data);
      })
    }
  }
});

// Notifications.vue
new Vue({
  mounted () {
    // you will want to add more than just an interval. You will want to keep track of this and perhaps stop it if, for example, the user logs out.
    setInterval(function () {
      store.dispatch('load');
    }.bind(this), 1000)
  }
});


// Add to your router
metaInfo: function () {
  return {
    title: '(' + store.state.notifications + ')' + 'Leads - ' + this.view
  }
}

这是一个使用Vuex解决此问题的快速示例。这不是测试,只是出于教育目的。更多信息请访问

这可以说是更好的方法,也是我在更大的应用程序中使用的方法。这可以说是更好的方法,也是我在更大的应用程序中使用的方法。