路由器视图中的控制器和父级控制器之间的两种方式绑定

编程入门 行业动态 更新时间:2024-10-12 18:19:41
本文介绍了路由器视图中的控制器和父级控制器之间的两种方式绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我有一个单页应用程序,其中包含:

I have a single page application which consists of a:

包含标题和菜单的导航组件路由器视图我希望在路由器视图中呈现与路由器中的状态相对应的每个组件,以更新导航组件的标题.如何将参数从路由器视图中的组件传递到外部导航组件.

A navigation component which contains a title and a menu A router-view I'd like each component being presented in the router view which correspond to a state in the router, to update the title of the navigation component. How to go about passing the parameters from the components in the router-view to the outer navigation component.

我使用的是 vue 2.0

I'm using vue 2.0

推荐答案

我会为此推荐 Vuex:

I would recommending Vuex for this:

https://github/vuejs/vuex

这样你就可以在主组件中访问你的 Vuex store 并显示它的标题:

This way you can access your Vuex store in the main component and display its title:

computed: {
  title () {
    return this.$store.state.title
  }
}

然后您将设置一个变更来更新标题并在您的组件中需要的任何地方调用它.现在,随着 Vue 的响应,导航组件中的标题将更新.

Then you'd set up a mutation to update the title and call this wherever required in your components. Now as Vue is reactive the title within the nav component will update.

下面是一个简短的例子:

Below is an abbreviated example:

Vuex 商店:

import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    title: ''
  },
  mutations: {
    updateTitle (state, title) {
      state.title = title
    }
  }
})

您的应用根目录:

import Vue from 'vue'
import store from './vuex/store'
new Vue({
  el: '#app',
  store,
})

导航组件:

export default {
  computed: {
    title () {
      return this.$store.state.title
    }
  },
  ...

任何其他需要更新您的标题的组件:

Any other component which needs to update your title:

import { mapMutations } from 'vuex'
export default {
  methods: {
    ...mapMutations([
      'updateTitle' // map this.updateTitle() to this.$storemit('updateTitle')
    ]),
  },
  // if you wanted to call it on mount
  mounted () {
    this.updateTitle('hello')
  },
  ...

这篇关于路由器视图中的控制器和父级控制器之间的两种方式绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-27 00:39:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1145618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控制器   两种   视图   绑定   路由器

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!