登录、注册、注销后跳转

使用this.$router.push({path: 'xxx'})跳转到指定页面

  1. onLogout(){
  2. auth.logout().then(() => this.$router.push({path: '/login'}))
  3. }
  1. onLogin() {
  2. auth.login({username: this.login.username, password: this.login.password})
  3. .then(() => {
  4. this.login.isError = false
  5. this.login.notice = ''
  6. this.$router.push({path: '/notebooks'})
  7. console.log('start redirect...')
  8. })
  9. .catch(data => {
  10. this.login.isError = true
  11. this.login.notice = data.msg
  12. })
  13. }

登录后获取用户信息

  1. <script lang="ts">
  2. import Vue from 'vue'
  3. import { Component } from 'vue-property-decorator';
  4. import Auth from '@/apis/auth'
  5. @Component
  6. export default class Avatar extends Vue{
  7. username = 'Valley'
  8. created(){
  9. Auth.get_info().then((res: any) => {
  10. if(res.isLogin) {
  11. this.username = res.data.username
  12. }
  13. })
  14. }
  15. get slug() {
  16. return this.username.charAt(0)
  17. }
  18. }
  19. </script>
  • slug使用计算属性
  • 获取数据写在created声明周期中

css使用text-transform设置大小写

  1. text-transform: uppercase;

组件间事件传递

在helpers目录中新建bus.ts,新建一个vue实例

  1. import Vue from 'vue'
  2. export default new Vue()

在Login组件中触发事件

  1. auth.login({username: this.login.username, password: this.login.password})
  2. .then(() => {
  3. this.login.isError = false
  4. this.login.notice = ''
  5. this.$router.push({path: '/notebooks'})
  6. // 触发事件
  7. Bus.$emit('login', {username: this.login.username})
  8. console.log('start redirect...')
  9. })

在Avatar组件中监听事件

  1. created(){
  2. // 监听事件
  3. Bus.$on('login', data => this.username = data.username)
  4. Auth.get_info().then((res: any) => {
  5. if(res.isLogin) {
  6. this.username = res.data.username
  7. }
  8. })
  9. }

未登录跳转到登录页面

  1. created() {
  2. Auth.get_info().then(res => {
  3. if (!res.isLogin) {
  4. this.$router.push('/login')
  5. }
  6. }
  7. )
  8. }

每个组件的this.$router都指向router

  1. import router from '@/router';
  2. @Component
  3. export default class Avatar extends Vue{
  4. created(){
  5. console.log(this.$router === router) // 结果为true
  6. }}

路由重构

路由改用query传参
router修改

  1. // ...
  2. {
  3. path: '/note',
  4. component: NoteDetail,
  5. },
  6. {
  7. path: '/trash',
  8. component: TrashDetail,
  9. },
  10. // ...

router-link传参

  1. <router-link :to="`/note?notebookId=${notebook.id}`" class="notebook" >

使用$route.query获取参数

  1. <div id="note-detail">
  2. <h1>notebookId: {{$route.query.notebookId}}</h1>
  3. <h1>noteId: {{$route.query.noteId}}</h1>
  4. </div>

回车登录

实现敲击回车时登录
在输入密码处的input监听回车事件@keyup.enter,触发登录

  1. <div :class="{show: isShowLogin}" class="login">
  2. <input type="text"
  3. v-model="login.username"
  4. placeholder="输入用户名">
  5. <input type="password"
  6. v-model="login.password"
  7. placeholder="密码"
  8. @keyup.enter="onLogin">
  9. <p v-bind:class="{error: login.isError}">{{login.notice}}</p>
  10. <div class="button"
  11. @click="onLogin">登录</div>
  12. </div>

如果不是在根组件上监听,需要使用.native修饰符