Skip to content
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

Update main.js #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
// 导入vue及VueRouter
import Vue from 'vue'
import routes from './routes'

// 创建和挂载根实例
const app = new Vue({
el: '#app',
data: {
// 指定当前路由
currentRoute: window.location.pathname
},
// 计算属性
computed: {
// 实例化一个视图组件
ViewComponent () {
// 根据当前路由去动态匹配视图
const matchingView = routes[this.currentRoute]
// 通过判断路由,返回对应的视图页面
return matchingView
? require('./pages/' + matchingView + '.vue')
: require('./pages/404.vue')
}
},
// 渲染函数,生成当前组件
render (h) {
return h(this.ViewComponent)
}
})

// 添加一个监听器,把当前窗口的路径名赋值给当前路由
window.addEventListener('popstate', () => {
app.currentRoute = window.location.pathname
})