我有以下小提琴https://jsfiddle.net/91vLms06/1/
const CreateComponent = Vue.component('create', {
props: ['user', 'otherProp'],
template: '<div>User data: {{user}}; other prop: {{otherProp}}</div>'
});
const ListComponent = Vue.component('List', {
template: '<div>Listing</div>'
});
const app = new Vue({
el: '#app',
router: new VueRouter(),
created: function () {
const self = this;
// ajax request returning the user
const userData = {'name': 'username'}
self.$router.addRoutes([
{ path: '/create', name: 'create', component: CreateComponent, props: { user: userData }},
{ path: '/list', name: 'list', component: ListComponent },
{ path: '*', redirect: …Run Code Online (Sandbox Code Playgroud) 我正在寻找如何在两个单独的组件(而不是父级和子级)之间秘密地传递数据,而不在我的Vue2应用程序中使用URL参数的方法。这并不意味着我要传递秘密,而是我不希望用户看到它(仅出于UI考虑)。
我知道Vue有Props,但它们旨在在父子组件之间传递数据。就我而言,我的URL会更改,但我不想通过可见参数传递数据。
有人声称这里使用没有URL参数的道具,但我无法复制有效的解决方案(每次都变得不确定)。
我还签出了这些选项,但是它们都使用了我们知道的URL或查询参数。
一个丑陋的解决方案是将数据写入本地存储,然后在其中读取,但这会产生大量的开销和复杂性(例如,如果我只希望一次读取该数据,等等)。
有没有更优雅的解决方案?
谢谢!