-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
289 additions
and
27 deletions.
There are no files selected for viewing
This file contains 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,78 @@ | ||
import Vue, { defineComponent, watch, ref, onUnmounted } from 'vue' | ||
import VueRouter from 'vue-router' | ||
import { useRoute, useRouter } from 'vue-router/composables' | ||
|
||
Vue.use(VueRouter) | ||
|
||
const Home = defineComponent({ | ||
setup () { | ||
const route = useRoute() | ||
const router = useRouter() | ||
|
||
// should be / | ||
const startRoute = route.fullPath | ||
|
||
console.log('got to Home', startRoute) | ||
|
||
const watchCount = ref(0) | ||
|
||
watch(() => route.query.n, () => { | ||
console.log('watched') | ||
watchCount.value++ | ||
}) | ||
|
||
onUnmounted(() => { | ||
console.log('unmounted') | ||
}) | ||
|
||
function navigate () { | ||
router.push({ query: { n: 1 + (Number(route.query.n) || 0) }}) | ||
} | ||
return { route, navigate, watchCount, startRoute } | ||
}, | ||
template: ` | ||
<div> | ||
<h2>Home</h2> | ||
<p id="start-route">{{ startRoute }}</p> | ||
<p id='watch-count'>{{ watchCount }}</p> | ||
<p id="fullpath">{{ route.fullPath }}</p> | ||
<button id="nav" @click="navigate">Navigate</button> | ||
</div> | ||
` | ||
}) | ||
|
||
const About = defineComponent({ | ||
setup () { | ||
const route = useRoute() | ||
return { route } | ||
}, | ||
template: ` | ||
<div> | ||
<h2>About</h2> | ||
<p id="fullpath">{{ route.fullPath }}</p> | ||
</div> | ||
` | ||
}) | ||
|
||
const router = new VueRouter({ | ||
mode: 'history', | ||
base: __dirname, | ||
routes: [ | ||
{ path: '/', component: Home }, | ||
{ path: '/about', component: About } | ||
] | ||
}) | ||
|
||
new Vue({ | ||
router, | ||
template: ` | ||
<div id="app"> | ||
<h1>Basic</h1> | ||
<ul> | ||
<li><router-link to="/">/</router-link></li> | ||
<li><router-link to="/about">/foo</router-link></li> | ||
</ul> | ||
<router-view class="view"></router-view> | ||
</div> | ||
` | ||
}).$mount('#app') |
This file contains 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,7 @@ | ||
<!DOCTYPE html> | ||
<link rel="stylesheet" href="/global.css"> | ||
<a href="/">← Examples index</a> | ||
<hr /> | ||
<div id="app"></div> | ||
<script src="/__build__/shared.chunk.js"></script> | ||
<script src="/__build__/composables.js"></script> |
This file contains 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 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 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 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,46 @@ | ||
import { getCurrentInstance, shallowReactive, effectScope } from 'vue' | ||
|
||
export function useRouter () { | ||
const i = getCurrentInstance() | ||
if (process.env.NODE_ENV !== 'production' && !i) { | ||
throwNoCurrentInstance('useRouter') | ||
} | ||
|
||
return i.proxy.$root.$router | ||
} | ||
|
||
export function useRoute () { | ||
const i = getCurrentInstance() | ||
if (process.env.NODE_ENV !== 'production' && !i) { | ||
throwNoCurrentInstance('useRoute') | ||
} | ||
|
||
const root = i.proxy.$root | ||
if (!root._$route) { | ||
const route = effectScope(true).run( | ||
() => shallowReactive(Object.assign({}, root.$router.currentRoute)) | ||
) | ||
root._$route = route | ||
|
||
root.$router.afterEach(to => { | ||
Object.assign(route, to) | ||
}) | ||
} | ||
|
||
return root._$route | ||
} | ||
|
||
// TODO: | ||
// export function useLink () {} | ||
|
||
// TODO: | ||
// export function onBeforeRouteUpdate () {} | ||
|
||
// TODO: | ||
// export function onBeforeRouteLeave () {} | ||
|
||
function throwNoCurrentInstance (method) { | ||
throw new Error( | ||
`[vue-router]: Missing current instance. ${method}() must be called inside <script setup> or setup().` | ||
) | ||
} |
This file contains 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,29 @@ | ||
const bsStatus = require('../browserstack-send-status') | ||
|
||
module.exports = { | ||
...bsStatus(), | ||
|
||
'@tags': ['history'], | ||
|
||
'useRoute() + useRouter()': function (browser) { | ||
browser | ||
.url('http://localhost:8080/composables/') | ||
.waitForElementVisible('#app', 1000) | ||
.assert.count('li', 2) | ||
.assert.count('li a', 2) | ||
.assert.containsText('.view', 'Home') | ||
|
||
.click('li:nth-child(2) a') | ||
.assert.containsText('.view', 'About') | ||
.click('li:nth-child(1) a') | ||
.assert.containsText('.view', 'Home') | ||
.assert.containsText('#start-route', '/') | ||
.assert.containsText('#fullpath', '/') | ||
|
||
.click('button#nav') | ||
.assert.containsText('#fullpath', '/?n=1') | ||
|
||
.end() | ||
} | ||
|
||
} |
Oops, something went wrong.