-
-
Notifications
You must be signed in to change notification settings - Fork 5k
1102: $route returns consistent value across all Vue instances #1108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import Vue from 'vue' | ||
import VueRouter from 'vue-router' | ||
|
||
// 1. Use plugin. | ||
// This installs <router-view> and <router-link>, | ||
// and injects $router and $route to all router-enabled child components | ||
Vue.use(VueRouter) | ||
|
||
// 2. Define route components | ||
const Home = { template: '<div>Component: home</div>' } | ||
const Foo = { template: '<div>Component: foo</div>' } | ||
const Bar = { template: '<div>Component: bar</div>' } | ||
|
||
// 3. Create the router | ||
const router = new VueRouter({ | ||
mode: 'history', | ||
base: __dirname, | ||
routes: [ | ||
{ path: '/', component: Home }, | ||
{ path: '/foo', component: Foo }, | ||
{ path: '/bar', component: Bar } | ||
] | ||
}) | ||
|
||
// 4. Create extended base Vue with router injected here (all | ||
// children should inherit the same router). | ||
const BaseVue = Vue.extend({ router }) | ||
|
||
// Discrete components means that a new Vue instance will be created | ||
// and bound on multiple *independent* nodes (eg. one Vue instance | ||
// per node); but the router should act as a singleton and keep all | ||
// instances in sync. | ||
document.querySelectorAll('.app').forEach((node) => { | ||
new BaseVue({ | ||
el: node | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!DOCTYPE html> | ||
<link rel="stylesheet" href="/global.css"> | ||
<style> | ||
.inliner {font-size:0;line-height:0;} | ||
.app {font-size:1rem;line-height:1;display:inline-block;padding:1rem;width:33%;border-left:1px solid #f1f1f1;box-sizing:border-box;vertical-align:top;} | ||
.snippet {display:inline-block;padding:5px;background:#f1f1f1;font-size:90%;} | ||
.app.component-view {display:block;width:100%;text-align:center;} | ||
</style> | ||
<a href="/">← Examples index</a> | ||
<div class="inliner"> | ||
<div class="app"> | ||
<ul> | ||
<li><router-link to="/">/</router-link></li> | ||
<li><router-link to="/foo">/foo</router-link></li> | ||
<li><router-link to="/bar">/bar</router-link></li> | ||
</ul> | ||
$route.path value: <span class="snippet">{{ $route.path }}</span> | ||
</div> | ||
<div class="app"> | ||
<ul> | ||
<li><router-link to="/">/</router-link></li> | ||
<li><router-link to="/foo">/foo</router-link></li> | ||
<li><router-link to="/bar">/bar</router-link></li> | ||
</ul> | ||
$route.path value: <span class="snippet">{{ $route.path }}</span> | ||
</div> | ||
<div class="app"> | ||
<ul> | ||
<li><router-link to="/">/</router-link></li> | ||
<li><router-link to="/foo">/foo</router-link></li> | ||
<li><router-link to="/bar">/bar</router-link></li> | ||
</ul> | ||
$route.path value: <span class="snippet">{{ $route.path }}</span> | ||
</div> | ||
</div> | ||
<div class="app component-view"> | ||
<router-view class="view"></router-view> | ||
$route.path value: <span class="snippet">{{ $route.path }}</span> | ||
</div> | ||
<script src="/__build__/shared.js"></script> | ||
<script src="/__build__/discrete-components.js"></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import Vue from 'vue' | ||
import VueRouter from '../../../src/index' | ||
|
||
describe('[Vue Instance].$route bindings', () => { | ||
describe('boundToSingleVueInstance', () => { | ||
it('updates $route on all instances', () => { | ||
const router = new VueRouter({ | ||
routes: [ | ||
{ path: '/', component: { name: 'foo' }}, | ||
{ path: '/bar', component: { name: 'bar' }} | ||
] | ||
}) | ||
const app1 = new Vue({ router }) | ||
const app2 = new Vue({ router }) | ||
expect(app1.$route.path).toBe('/') | ||
expect(app2.$route.path).toBe('/') | ||
router.push('/bar') | ||
expect(app1.$route.path).toBe('/bar') | ||
expect(app2.$route.path).toBe('/bar') | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A check shouldn't be necessary, should it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thats what I thought as well, but the syntax check wouldn't let it pass. Don't remember exactly, but it was along the lines of "Cannot set property on a possibly null value".