From 1efd664fbb03aad0d7e2e613eaad73df1a1ca455 Mon Sep 17 00:00:00 2001 From: lhx <781131860@qq.com> Date: Tue, 15 Jun 2021 11:42:37 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E7=AC=94=E8=AE=B0=E6=8F=90=E4=BA=A4?= =?UTF-8?q?=E5=93=A6=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\346\236\227\351\270\277\351\221\253/note2021-6-09-01.md" | 1 - "\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" | 4 ++++ 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 "\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" diff --git "a/\346\236\227\351\270\277\351\221\253/note2021-6-09-01.md" "b/\346\236\227\351\270\277\351\221\253/note2021-6-09-01.md" index 6201719..f887b8f 100644 --- "a/\346\236\227\351\270\277\351\221\253/note2021-6-09-01.md" +++ "b/\346\236\227\351\270\277\351\221\253/note2021-6-09-01.md" @@ -24,7 +24,6 @@ yarn global add @vue/cli cd D:\myGitProjects\ - vue my-vue-app vue create my-vue-app diff --git "a/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" "b/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" new file mode 100644 index 0000000..dd33290 --- /dev/null +++ "b/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" @@ -0,0 +1,4 @@ +# 路由注册 + +就先yarn serve +然后再终端执行 vue add router \ No newline at end of file -- Gitee From 39ab7d267eddc673701114c4f0fa938292c09cc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cglinhongxin?= <781131860@qq.com> Date: Tue, 15 Jun 2021 13:16:56 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=86=85=E5=AE=B9=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../note2021-6-15-01.md" | 49 ++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git "a/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" "b/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" index dd33290..7ee6741 100644 --- "a/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" +++ "b/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" @@ -1,4 +1,51 @@ # 路由注册 就先yarn serve -然后再终端执行 vue add router \ No newline at end of file +然后再终端执行 vue add router + +下面就是路由注册的操作: + +App.vue里面的: + + + + +下面是main.js: + + import Vue from 'vue' + import App from './App.vue' + import VueRouter from 'vue-router' //引入路由 + import Login from './components/Login.vue'; //引入组件 + + Vue.use(VueRouter); //Vue.use加入(引入的路由) + + var routes = new VueRouter({ //实例化路由 + mode:'history', + routes: [ + { + path: '/login', //注册的路由 + component: Login //注册的路由显示的组件 + } + ] + }) + + + Vue.config.productionTip = false + + + new Vue({ + router: routes, //注册该路由(如果实例化路由为router,则这可以直接简写为router) + render: h => h(App), + }).$mount('#app') -- Gitee From 99240df115a62941b89d70b387c4ae792604a913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E9=B8=BF=E9=91=AB?= <781131860@qq.com> Date: Tue, 15 Jun 2021 13:17:47 +0800 Subject: [PATCH 3/5] =?UTF-8?q?update=20=E6=9E=97=E9=B8=BF=E9=91=AB/note20?= =?UTF-8?q?21-6-15-01.md.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- "\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git "a/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" "b/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" index 7ee6741..481bb08 100644 --- "a/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" +++ "b/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" @@ -1,7 +1,7 @@ # 路由注册 就先yarn serve -然后再终端执行 vue add router +然后再终端执行 yarn vue add router 下面就是路由注册的操作: -- Gitee From 8f2f32619cad181b1768150d643a76651d47c538 Mon Sep 17 00:00:00 2001 From: lhx <781131860@qq.com> Date: Wed, 16 Jun 2021 17:34:06 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E7=AC=AC14=E6=AC=A1=E8=AF=BE=E7=AC=94?= =?UTF-8?q?=E8=AE=B0=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../note2021-6-12-01.md" | 2 +- .../note2021-6-15-01.md" | 4 +- .../note2021-6-16-01.md" | 187 ++++++++++++++++++ 3 files changed, 190 insertions(+), 3 deletions(-) create mode 100644 "\346\236\227\351\270\277\351\221\253/note2021-6-16-01.md" diff --git "a/\346\236\227\351\270\277\351\221\253/note2021-6-12-01.md" "b/\346\236\227\351\270\277\351\221\253/note2021-6-12-01.md" index 21a5cbe..9b1c3e1 100644 --- "a/\346\236\227\351\270\277\351\221\253/note2021-6-12-01.md" +++ "b/\346\236\227\351\270\277\351\221\253/note2021-6-12-01.md" @@ -1,4 +1,4 @@ -# 单文件组件 +# 单文件组件(12次课) 在很多 Vue 项目中,我们使用 Vue.component 来定义全局组件,紧接着用 new Vue({ el: '#container '}) 在每个页面内指定一个容器元素。 diff --git "a/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" "b/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" index 481bb08..4f14b91 100644 --- "a/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" +++ "b/\346\236\227\351\270\277\351\221\253/note2021-6-15-01.md" @@ -1,7 +1,7 @@ -# 路由注册 +# 路由注册(13次课) 就先yarn serve -然后再终端执行 yarn vue add router +然后再终端执行 yarn add vue-router 下面就是路由注册的操作: diff --git "a/\346\236\227\351\270\277\351\221\253/note2021-6-16-01.md" "b/\346\236\227\351\270\277\351\221\253/note2021-6-16-01.md" new file mode 100644 index 0000000..78d615a --- /dev/null +++ "b/\346\236\227\351\270\277\351\221\253/note2021-6-16-01.md" @@ -0,0 +1,187 @@ +# 动态路由匹配(14次课) + +我们有一个组件,对于所有 ID 各不相同的用户,都要使用这个组件来渲染。那么,我们可以在 vue-router 的路由路径中使用“动态路径参数”(dynamic segment) 来达到这个效果: + +

{{ $route.params.id }}

+ + let router = new VueRouter({ + mode: 'history', + routes: + [ + { path: '/person/:id', component: Person } + ] + }) + +这里的id可以换成任何东西,但是在{{ $route.params.id }}里面的id还要跟路径的要一模一样才行! + +你可以在一个路由中设置多段“路径参数”,对应的值都会设置到 $route.params 中。例如: + +模式 匹配路径 $route.params +/user/:username /user/evan { username: 'evan' } +/user/:username/post/:post_id /user/evan/post/123 { username: 'evan', post_id: '123' } +除了 $route.params 外,$route 对象还提供了其它有用的信息,例如,$route.query (如果 URL 中有查询参数)、$route.hash 等等。 + + +## 响应路由参数的变化 + +提醒一下,当使用路由参数时,例如从 /user/foo 导航到 /user/bar,原来的组件实例会被复用。因为两个路由都渲染同个组件,比起销毁再创建,复用则显得更加高效。不过,这也意味着组件的生命周期钩子不会再被调用。 + + watch: { + $route(to, from) { + console.log(to); + console.log(from); + }, + }, + beforeRouteUpdate(to, from, next) { + console.log(to); + console.log(from); + console.log(next); + }, + +这个两个胡歌还没解决还不懂! + + +## 捕获所有路由或 404 Not found 路由 + +常规参数只会匹配被 / 分隔的 URL 片段中的字符。如果想匹配任意路径,我们可以使用通配符 (*): + + {path:'*',component:p404}(可以匹配任何路径) + + + +当使用通配符路由时,请确保路由的顺序是正确的,也就是说含有通配符的路由应该放在最后。 + +当使用一个通配符时,$route.params 内会自动添加一个名为 pathMatch 参数。它包含了 URL 通过通配符被匹配的部分: + +// 给出一个路由 { path: '/user-*' } +this.$router.push('/user-admin') +this.$route.params.pathMatch // 'admin' +// 给出一个路由 { path: '*' } +this.$router.push('/non-existing') +this.$route.params.pathMatch // '/non-existing' + +## 匹配优先级 +有时候,同一个路径可以匹配多个路由,此时,匹配的优先级就按照路由的定义顺序:路由定义得越早,优先级就越高。 + +# 嵌套路由 + + + + +

{{ $route.params.id }}

+ + + { path: '/person', component: Person, children: [{ path: 'name', component: sss }] }, + +这里的 是最顶层的出口,渲染最高级路由匹配到的组件。同样地,一个被渲染组件同样可以包含自己的嵌套 。 +要在嵌套的出口中渲染组件,需要在 VueRouter 的参数中使用 children 配置. +要注意,以 / 开头的嵌套路径会被当作根路径。 这让你充分的使用嵌套组件而无须设置嵌套的路径。 + +你会发现,children 配置就是像 routes 配置一样的路由配置数组,所以呢,你可以嵌套多层路由。 + +今天的笔记例子: + +person.vue: + + + + + + + + +main.js: + + import Vue from 'vue' + import App from './App.vue' + import VueRouter from 'vue-router' + import Person from './components/Person' + import p404 from './components/404' + import sss from './components/sss' + Vue.config.productionTip = false + + Vue.use(VueRouter) + let router = new VueRouter({ + mode: 'history', + routes: + [ + { path: '/person', component: Person, children: [{ path: 'name', component: sss }] }, + { path: '*', component: p404 } + ] + }) + + + new Vue({ + router, + render: h => h(App), + }).$mount('#app') + +sss.vue: + + + + + + + +404.vue: + + + + + + \ No newline at end of file -- Gitee From e42f9e1ecd04fcc4edee25ab95a5c3e6d94cdc6e Mon Sep 17 00:00:00 2001 From: lhx <781131860@qq.com> Date: Sat, 19 Jun 2021 17:39:51 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E4=BB=8A=E6=97=A5=E7=AC=94=E8=AE=B0?= =?UTF-8?q?=EF=BC=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../note2021-6-18-01.md" | 163 +++++++++++++++ .../note2021-6-19-01.md" | 187 ++++++++++++++++++ 2 files changed, 350 insertions(+) create mode 100644 "\346\236\227\351\270\277\351\221\253/note2021-6-18-01.md" create mode 100644 "\346\236\227\351\270\277\351\221\253/note2021-6-19-01.md" diff --git "a/\346\236\227\351\270\277\351\221\253/note2021-6-18-01.md" "b/\346\236\227\351\270\277\351\221\253/note2021-6-18-01.md" new file mode 100644 index 0000000..685f38b --- /dev/null +++ "b/\346\236\227\351\270\277\351\221\253/note2021-6-18-01.md" @@ -0,0 +1,163 @@ +# 编程式的导航 + +除了使用 创建 a 标签来定义导航链接,我们还可以借助 router 的实例方法,通过编写代码来实现。 + +## router.push(location, onComplete?, onAbort?) + +在 Vue 实例内部,你可以通过 $router 访问路由实例。因此你可以调用 this.$router.push。 + +想要导航到不同的 URL,则使用 router.push 方法。这个方法会向 history 栈添加一个新的记录,所以,当用户点击浏览器后退按钮时,则回到之前的 URL。 + +当你点击 时,这个方法会在内部调用,所以说,点击 等同于调用 router.push(...)。 + + import Vue from 'vue' + import App from './App.vue' + + import VueRouter from 'vue-router' + + import user from './components/user' + import Login from './components/Login' + + Vue.config.productionTip = false + Vue.use(VueRouter) + let router = new VueRouter({ + mode: 'history', + routes: [ + { path: '/user', component: user }, + { path:'/login', component:Login} + ] + }) + new Vue({ + router, + render: h => h(App), + }).$mount('#app') + + + +App.vue: + + + + + + + + +user.vue: + + + + + + + +Login.vue : + + + + + + + +该方法的参数可以是一个字符串路径,或者一个描述地址的对象。例如: + +// 字符串 +router.push('home') + +// 对象 +router.push({ path: 'home' }) + +// 命名的路由 +router.push({ name: 'user', params: { userId: '123' }}) + +// 带查询参数,变成 /register?plan=private +router.push({ path: 'register', query: { plan: 'private' }}) +注意:如果提供了 path,params 会被忽略,上述例子中的 query 并不属于这种情况。取而代之的是下面例子的做法,你需要提供路由的 name 或手写完整的带有参数的 path: + +const userId = '123' +router.push({ name: 'user', params: { userId }}) // -> /user/123 +router.push({ path: `/user/${userId}` }) // -> /user/123 +// 这里的 params 不生效 +router.push({ path: '/user', params: { userId }}) // -> /user + +## router.replace(location, onComplete?, onAbort?) + +跟 router.push 很像,唯一的不同就是,它不会向 history 添加新记录,而是跟它的方法名一样 —— 替换掉当前的 history 记录。 + + 声明式 编程式 + router.replace(...) + + +## router.go(n) + +这个方法的参数是一个整数,意思是在 history 记录中向前或者后退多少步,类似 window.history.go(n)。 + +例子 + +// 在浏览器记录中前进一步,等同于 history.forward() +router.go(1) + +// 后退一步记录,等同于 history.back() +router.go(-1) + +// 前进 3 步记录 +router.go(3) + +// 如果 history 记录不够用,那就默默地失败呗 +router.go(-100) +router.go(100) \ No newline at end of file diff --git "a/\346\236\227\351\270\277\351\221\253/note2021-6-19-01.md" "b/\346\236\227\351\270\277\351\221\253/note2021-6-19-01.md" new file mode 100644 index 0000000..c27c9f0 --- /dev/null +++ "b/\346\236\227\351\270\277\351\221\253/note2021-6-19-01.md" @@ -0,0 +1,187 @@ +# 命名路由 + +const router = new VueRouter({ + routes: [ + { + path: '/user/:userId', + name: 'user', + component: User + } + ] +}) + +这就是简单粗爆: + + +要链接到一个命名路由,可以给 router-link 的 to 属性传一个对象: + +User + +这跟代码调用 router.push() 是一回事: + +router.push({ name: 'user', params: { userId: 123 } }) + +# 命名视图 + +有时候想同时 (同级) 展示多个视图,而不是嵌套展示: + +我来告诉你: + + + + + + + + const router = new VueRouter({ + routes: [ + { + path: '/user', + name:'user' + components: { + user:user, + login:login, + register: register, + person:person + } + } + ] + }) + +## 嵌套命名视图 + +
+

User Settings

+ + + +
+ +{ + path: '/settings', + // 你也可以在顶级路由就配置命名视图 + component: UserSettings, + children: [{ + path: 'emails', + component: UserEmailsSubscriptions + }, { + path: 'profile', + components: { + default: UserProfile, + helper: UserProfilePreview + } + }] +} + +## 重定向和别名 + +重定向也是通过 routes 配置来完成,下面例子是从 /a 重定向到 /b: + +const router = new VueRouter({ + routes: [ + { path: '/a', redirect: '/b' } + ] +}) +重定向的目标也可以是一个命名的路由: + +const router = new VueRouter({ + routes: [ + { path: '/a', redirect: { name: 'foo' }} + ] +}) +甚至是一个方法,动态返回重定向目标: + +const router = new VueRouter({ + routes: [ + { path: '/a', redirect: to => { + // 方法接收 目标路由 作为参数 + // return 重定向的 字符串路径/路径对象 + }} + ] +}) + +## 别名 + +“重定向”的意思是,当用户访问 /a时,URL 将会被替换成 /b,然后匹配路由为 /b,那么“别名”又是什么呢? + +/a 的别名是 /b,意味着,当用户访问 /b 时,URL 会保持为 /b,但是路由匹配则为 /a,就像用户访问 /a 一样。 + +上面对应的路由配置为: + +const router = new VueRouter({ + routes: [ + { path: '/a', component: A, alias: '/b' } + ] +}) + +## 路由组件传参 + + +const User = { + props: ['id'], + template: '
User {{ id }}
' +} +const router = new VueRouter({ + routes: [ + { path: '/user/:id', component: User, props: true }, + + // 对于包含命名视图的路由,你必须分别为每个命名视图添加 `props` 选项: + { + path: '/user/:id', + components: { default: User, sidebar: Sidebar }, + props: { default: true, sidebar: false } + } + ] +}) + +## 布尔模式 +如果 props 被设置为 true,route.params 将会被设置为组件属性。 + + +## 对象模式 +如果 props 是一个对象,它会被按原样设置为组件属性。当 props 是静态的时候有用。 + +const router = new VueRouter({ + routes: [ + { + path: '/promotion/from-newsletter', + component: Promotion, + props: { newsletterPopup: false } + } + ] +}) + +## 函数模式 +你可以创建一个函数返回 props。这样你便可以将参数转换成另一种类型,将静态值与基于路由的值结合等等。 + +const router = new VueRouter({ + routes: [ + { + path: '/search', + component: SearchUser, + props: route => ({ query: route.query.q }) + } + ] +}) +URL /search?q=vue 会将 {query: 'vue'} 作为属性传递给 SearchUser 组件。 + +请尽可能保持 props 函数为无状态的,因为它只会在路由发生变化时起作用。如果你需要状态来定义 props,请使用包装组件,这样 Vue 才可以对状态变化做出反应。 + +## HTML5 History 模式 + +#nginx +location / { + try_files $uri $uri/ /index.html; +} + +## 警告 +给个警告,因为这么做以后,你的服务器就不再返回 404 错误页面,因为对于所有路径都会返回 index.html 文件。为了避免这种情况,你应该在 Vue 应用里面覆盖所有的路由情况,然后再给出一个 404 页面。 + +const router = new VueRouter({ + mode: 'history', + routes: [ + { path: '*', component: NotFoundComponent } + ] +}) + +或者,如果你使用 Node.js 服务器,你可以用服务端路由匹配到来的 URL,并在没有匹配到路由的时候返回 404,以实现回退 \ No newline at end of file -- Gitee