javascript - VueJS 组件显示并将项目传递给组件

标签 javascript vue.js vuejs2 vue-router

我正在使用 VueJS 2.0 和 vue-router 2,并尝试显示基于路由参数的模板。我正在使用一个 View (WidgetView) 并更改该 View 中显示的组件。最初,我显示一个小部件列表组件(WidgetComponent),然后当用户在 WidgetView 中的 WidgetComponent 中选择一个小部件或新按钮时,我想交换 WidgetComponent 并显示 WidgetDetails 组件,并将信息传递给该组件:

WidgetComponent.vue:

<template>
...
<router-link :to="{ path: '/widget_view', params: { widgetId: 'new' }  }"><a> New Widget</a></router-link>
<router-link :to="{ path: '/widget_view', params: { widgetId: widget.id } }"><span>{{widget.name}}</span></router-link>
</template>
<script>
  export default {
    name: 'WidgetComponent',
    data() {
      return {
        widgets: [{ id: 1,
        name: 'widgetX',
        type: 'catcher'
      }]}
    }
  }
</script>

WidgetView.vue

<template>
  <component :is="activeComponent"></component>
</template>
<script>
  import WidgetComponent from './components/WidgetComponent'
  import WidgetDetail from './components/WidgetDetail'
  export default {
    name: 'WidgetView',
    components: {
      WidgetComponent,
      WidgetDetail
    },
    mounted: function () {
      const widgetId = this.$route.params.widgetId
      if (widgetId === 'new') {
        // I want to pass the id to this component but don't know how
        this.activeComponent = 'widget-detail'
      }
      else if (widgetId > 0) {
        // I want to pass the id to this component but don't know how
        this.activeComponent = 'widget-detail'
      }
    },
    watch: {
      '$route': function () {
        if (this.$route.params.widgetId === 'new') {
          // how to pass id to this compent?
          this.activeComponent = 'widget-detail'
        }
        else if (this.$route.params.widgetId > 0){
          // how to pass id to this compent?
          this.activeComponent = 'widget-detail'
        }
        else {
          this.activeComponent = 'widget-component'
        }
      }
    },
    data () {
      return {
        activeComponent: 'widget-component',
        widgetId: 0
      }
    }
  }
</script>

WidgetDetail.vue

<template>
<option v-for="manufacturer in manufacturers" >
    {{ manufacturer.name }}
</option>
</template>
<script>
   export default {
     props: ['sourcesId'],
     ...etc...
   }
</script>

路由器.js

Vue.use(Router)

export default new Router({
  routes: [
    {
      path: '/widget_view',
      component: WidgetView,
      subRoutes: {
        path: '/new',
        component: WidgetDetail
      }
    },
    {
      path: '/widget_view/:widgetId',
      component: WidgetView
    },
  ]

})

我无法让路由参数工作,但我设法通过对路由进行硬编码来使路由工作

<router-link :to="{ path: '/widget_view/'+ 'new' }"> New Widget</router-link>

但我不知道如何从 WidgetView 中的脚本(而不是模板)代码将 id 传递给给定模板。

最佳答案

这是一个基本示例 http://jsfiddle.net/ognc78e7/1/ 。尝试使用 router-view 元素来保存您的组件。另外,在组件内使用 props 从 URL 传递变量。文档解释得更好 http://router.vuejs.org/en/essentials/passing-props.html

//routes
{ path: '/foo/:id', component: Bar, props:true }
//component
const Bar = { template: '<div>The id is {{id}}</div>',props:['id'] }

不确定您想要哪种方式,但您可以将 /foo/ 路径实际上作为创建小部件,然后使用动态 /foo/:id 路径。或者您可以像我在这里所做的那样,foo 路径就像链接到不同内容的起始页。

关于javascript - VueJS 组件显示并将项目传递给组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42847911/

相关文章:

vue.js - 如何修复@vue/cli 漏洞?

vue.js - Vue3 路由器 + 转换警告

JavaScript 与星号的模式匹配

javascript - 使用 AngularJS 设置事件标签样式

java - jQuery timepicker 插件不适用于 struts2-jquery

javascript - VueJS可以多个元素调用同一个方法

javascript - 如何在vuejs项目中导入并使用luxon?

javascript - 如何在javascript文件之间共享数据?

docker - 使用docker在其他端口上运行Vue.js App

javascript - Vuejs - 未在实例上定义计算属性