你会注意到你的 Quasar 项目包含一个 /src/router
文件夹。这保存了你的网站/应用的路由配置:
¥You’ll notice that your Quasar project contains a /src/router
folder. This holds the routing configuration of your website/app:
“/src/router/index.js” 拥有 Vue 路由初始化代码
¥“/src/router/index.js” holds the Vue Router initialization code
“/src/router/routes.js” 保存你的网站/应用的路由
¥“/src/router/routes.js” holds the routes of your website/app
警告
Quasar 文档假设你已经熟悉 Vue 路由。下面仅描述了如何在 Quasar CLI 项目中使用它的基本方法。有关其功能的完整列表,请访问 Vue 路由文档。
¥Quasar documentation assumes you are already familiar with Vue Router. Below it’s described only the basics of how to make use of it in a Quasar CLI project. For the full list of its features please visit the Vue Router documentation.
/src/router/routes.js
需要导入你的网站/应用的页面和布局。在 使用布局和页面进行路由 文档页面上阅读更多内容。
¥The /src/router/routes.js
needs to import your website/app’s Pages and Layouts. Read more on Routing with Layouts and Pages documentation page.
使用 Pinia 时,存储不能直接从其他脚本导入,但它会被传递给 /src/router/index.js
的导出函数,因此可以从那里访问。例如,你可以使用 Router.beforeEach
方法在路由中检查身份验证:
¥When using Pinia, the store is not directly importable from other scripts, but it is passed to the exported function of /src/router/index.js
, so it can be accessed from there. For example you can use the Router.beforeEach
method to check authentication in the router:
import { defineRouter } from '#q-app/wrappers'
export default defineRouter(({ store /*, ssrContext */ }) => {
// ...
const userStore = useUserStore(store);
Router.beforeEach((to, from, next) => {
if (to.matched.some(record => record.meta.requiresAuth) && !userStore.isSignedIn) {
next({ name: 'account-signin', query: { next: to.fullPath } })
} else {
next()
}
})
// ...
})
提示
如果你正在开发服务器端渲染 (SSR) 应用,则可以查看服务器端提供的 ssrContext 对象。
¥If you are developing a SSR app, then you can check out the ssrContext Object that gets supplied server-side.