Skip to content

Commit

Permalink
migration: update global mounting API usage
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 5, 2021
1 parent b05d955 commit a6e0c9a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 93 deletions.
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ title }}</title>
<title>Vue HN</title>
<meta charset="utf-8">
<meta name="mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-capable" content="yes">
Expand All @@ -19,6 +19,7 @@
<body>
<div id="skip"><a href="#app">skip to content</a></div>
<div id="app"></div>
<script type="module" src="/src/entry-client.js"></script>
<div id="progress-container"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
35 changes: 0 additions & 35 deletions src/app.js

This file was deleted.

56 changes: 0 additions & 56 deletions src/entry-client.js

This file was deleted.

68 changes: 68 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { createApp } from 'vue'
import { createStore } from './store'
import { createRouter } from './router'
import { sync } from 'vuex-router-sync'
import titleMixin from './util/title'
import App from './App.vue'
import ProgressBar from './components/ProgressBar.vue'

const store = createStore()
const router = createRouter()

sync(store, router)

const app = createApp({
router,
store,
...App
})

// global progress bar
const bar = createApp(ProgressBar).mount('#progress-container')
document.body.appendChild(bar.$el)
app.config.globalProperties.$bar = bar

// mixin for handling title
app.mixin(titleMixin)

// a global mixin that calls `asyncData` when a route component's params change
app.mixin({
beforeRouteUpdate(to, from, next) {
const { asyncData } = this.$options
if (asyncData) {
asyncData({
store: this.$store,
route: to
})
.then(next)
.catch(next)
} else {
next()
}
}
})

// Add router hook for handling asyncData.
router.beforeResolve((to, from, next) => {
const matched = router.getMatchedComponents(to)
const prevMatched = router.getMatchedComponents(from)
let diffed = false
const activated = matched.filter((c, i) => {
return diffed || (diffed = prevMatched[i] !== c)
})
const asyncDataHooks = activated.map((c) => c.asyncData).filter((_) => _)
if (!asyncDataHooks.length) {
return next()
}

bar.start()
Promise.all(asyncDataHooks.map((hook) => hook({ store, route: to })))
.then(() => {
bar.finish()
next()
})
.catch(next)
})

// actually mount to DOM
app.mount('#app')

1 comment on commit a6e0c9a

@yyx990803
Copy link
Member Author

@yyx990803 yyx990803 commented on a6e0c9a May 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Migrate the app mounting to use Vue 3 API. We are merging the previous entry-client.js and app.js into a single file since the separation was only for SSR purposes.

The main change is that instead of new Vue, we now create an app instance with createApp. Global mixins are now applied to the app instance with app.mixin instead of using Vue.mixin.

router.onReady usage is also removed. router.onReady won't fire after we migrate to Vue 3's createApp since the root instance isn't created until app.mount is called, which in turn won't trigger the ready state of the router. The usage of router.onReady was for SSR purpose anyway, so we can safely remove it here.

Docs: https://v3.vuejs.org/guide/migration/global-api.html#a-new-global-api-createapp

Please sign in to comment.