我有一个使用 vue.js 和 vue-router 的原型。
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
let router = new VueRouter
import App from './App.vue'
import Details from './Details.vue'
import Pics from './Pics.vue'
router.map({
'/details':{
name: 'details',
component: Details
},
'/pictures':{
name: 'pics',
component: Pics
}
})
router.start(App, '#app')
App.vue 包含我的router-view
安装点。App 还包含一个Location
基于存储在我的共享状态中的位置数据的组件列表:
应用程序.vue:
<script>
import state from './state.js'
import Location from './Location.vue'
module.exports = {
components: {Location},
data: () => {
return {
name: 'app',
locations: state.locations
}
}
}
</script>
<template>
<div class="app-container">
<h2 class="heading">Locations</h2>
<div class="body">
<div class="column">
<location v-for="location in locations" :location="location"></location>
</div>
<div class="column">
<router-view></router-view>
</div>
</div>
</div>
</template>
state.js:
exports.locations = [
{ name: 'Home', address: '123 main st', pics: [{src: '/images/123.png'}]},
{ name: 'Work', address: '555 Avenue st', pics: [{src: '/images/lol.jpg'}, {src: '/images/wrkn.jpg'}]},
{ name: 'Some Landmark', address: '42 E West st', pics: [{src: '/images/coolpic.jpg'}, {src: '/images/qwerty.jpg'}]}
]
该Location.vue
组件包含v-links
我用来触发路由器的组件:
位置.vue:
<script>
module.exports = {
props:['location'],
data: () => {
return {name: 'location'}
}
}
</script>
<template>
<div class="location-container">
<h3>{{location.name}}</h3>
<a v-link="{name: 'pics'}">Pics</a>
<a v-link="{name: 'details'}">Details</a>
</div>
</template>
当单击特定位置时,我想要将给定的位置状态实例作为道具传递给路由器调用的组件(要么 要么Details.vue
)。Pics.vue
v-link
我遇到的问题是,虽然我知道文档说你可以做到这一点,但他们没有解释如何做到这一点,我也无法弄清楚如何做到这一点。我尝试将位置绑定到router-view
in App.vue
:
<router-view :location="location"></router-view>
并将其绑定到内的占位符router-view
:
<router-view>
<details :location="location"></location>
</router-view>
两者都不起作用。
这样做的正确方法是什么?
我在这里有一个问题的 WebpackBin 演示:http ://www.webpackbin.com/4kDRbt28W