vue 3路由 带注解 文章是对某网站的教程加注释介绍

上代码:

<script>
import Home from './components/Home.vue'
import About from './components/About.vue'
import NotFound from './components/NotFound.vue'

const routes = {
  '/': Home,
  '/about': About
}

export default {
  data() {
    return {
      //通过 window.location. hash = hash 这个语句来调整地址栏的地址
      //使得浏览器里边的“前进”、“后退”按钮能正常使用(实质上欺骗了浏览器)。
      currentPath: window.location.hash
    }
  },
  computed: {
    //返回一个组件
    currentView() {//slice 函数[1,字符串长度) 如果没有值 为/  理解为 前两个组件都不是,就第三个组件
      return routes[this.currentPath.slice(1) || '/'] || NotFound
    }
  },
  mounted() {
    //addEventListener 方法 对 window 对象调用 addEventListener 方法,
    // 注册一个加载事件处理程序 window.addEventListener("load", init, false) ; function init(){ //网页加载完毕后的事件处理程序 }
    
    //window.location.hash值的变化以及浏览器地址栏(#号后面的值发生变化)任何一方发生变化,
    // 都会触发onhashchange事件 hashchange事件
    window.addEventListener('hashchange', () => {
      this.currentPath = window.location.hash
    })
  }
}
</script>

<template>
  <a href="#/">Home</a> |
  <a href="#/about">About</a> |
  <a href="#/non-existent-path">Broken Link</a>

  <!-- 使用component 占位符来展示组件 -->
  <!-- 注意 :is 是绑定的属性,需要在实例的data中绑定的 组件的名称是字符串 -->
  <!-- 其中 他还提供了 mode属性来切换动画的先进先出的问题  -->
  <component :is="currentView" />
  <!--个人理解 这里的component 是展示 组件的内容 专门来展示用的 固定的 is 是绑定了一个函数 这个函数的功能改变浏览器的地址栏的地址-->
</template>

具体解释看注解,要被气死了

连接 具体的代码在这里:

Vue SFC Playground

component 标签解释:Vue组件(35)动态组件 component 的 is 到底可以是啥? - 金色海洋(jyk) - 博客园

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里以一个简单的待办事项列表为例,实现增删改查功能。 1. 创建 Vue 组件 在 src/components 目录下创建 TodoList.vue 组件,代码如下: ```html <template> <div> <h2>待办事项列表</h2> <ul> <li v-for="(todo, index) in todos" :key="todo.id"> {{ todo.text }} <button @click="deleteTodo(index)">删除</button> </li> </ul> <form @submit.prevent="addTodo"> <input type="text" v-model="newTodoText" placeholder="添新的待办事项"> <button type="submit">添</button> </form> </div> </template> <script> export default { data() { return { todos: [ { id: 1, text: '学习 Vue.js' }, { id: 2, text: '学习 Vuex' }, { id: 3, text: '学习 Vue Router' } ], newTodoText: '' } }, methods: { addTodo() { if (this.newTodoText.trim()) { const newTodo = { id: Date.now(), text: this.newTodoText.trim() } this.todos.push(newTodo) this.newTodoText = '' } }, deleteTodo(index) { this.todos.splice(index, 1) } } } </script> ``` 这是一个简单的待办事项列表组件,包含了待办事项的展示、添和删除功能。todos 数组存储了待办事项列表,newTodoText 存储了新待办事项的文本。 2. 创建 Vue 路由 在 src/router/index.js 文件中创建路由,代码如下: ```js import Vue from 'vue' import VueRouter from 'vue-router' import TodoList from '../components/TodoList.vue' Vue.use(VueRouter) const routes = [ { path: '/', name: 'TodoList', component: TodoList } ] const router = new VueRouter({ mode: 'history', base: process.env.BASE_URL, routes }) export default router ``` 这里只创建了一个路由,指向了 TodoList 组件。 3. 创建 Vuex Store 在 src/store/index.js 文件中创建 Vuex Store,代码如下: ```js import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) export default new Vuex.Store({ state: { todos: [ { id: 1, text: '学习 Vue.js' }, { id: 2, text: '学习 Vuex' }, { id: 3, text: '学习 Vue Router' } ] }, mutations: { addTodo (state, newTodoText) { if (newTodoText.trim()) { const newTodo = { id: Date.now(), text: newTodoText.trim() } state.todos.push(newTodo) } }, deleteTodo (state, index) { state.todos.splice(index, 1) } }, actions: { addTodo ({ commit }, newTodoText) { commit('addTodo', newTodoText) }, deleteTodo ({ commit }, index) { commit('deleteTodo', index) } }, getters: { todos: state => state.todos } }) ``` 这里的 state 对象中存储了待办事项列表,mutations 中定义了添和删除待办事项的方法,actions 中定义了对应的异步操作,getters 中定义了获取待办事项列表的方法。 4. 在组件中使用 Vuex 在 TodoList.vue 组件中使用 Vuex,代码如下: ```html <template> <div> <h2>待办事项列表</h2> <ul> <li v-for="(todo, index) in todos" :key="todo.id"> {{ todo.text }} <button @click="deleteTodo(index)">删除</button> </li> </ul> <form @submit.prevent="addTodo"> <input type="text" v-model="newTodoText" placeholder="添新的待办事项"> <button type="submit">添</button> </form> </div> </template> <script> import { mapGetters, mapActions } from 'vuex' export default { computed: { ...mapGetters([ 'todos' ]) }, data() { return { newTodoText: '' } }, methods: { ...mapActions([ 'addTodo', 'deleteTodo' ]) } } </script> ``` 这里使用了 Vuex 的辅助函数 mapGetters 和 mapActions,分别将 todos 数组和添、删除待办事项的方法映射到组件的 computed 和 methods 中。 5. 使用 Axios 获取数据 在 src/services/todoService.js 文件中定义获取待办事项列表的方法,代码如下: ```js import axios from 'axios' const API_URL = 'http://localhost:3000/todos' export function getTodos() { return axios.get(API_URL) .then(response => response.data) } ``` 这里使用了 Axios 库发送 HTTP 请求,获取待办事项列表。 6. 在组件中使用 Axios 和 Vuex 在 TodoList.vue 组件中使用 Axios 和 Vuex,代码如下: ```html <template> <div> <h2>待办事项列表</h2> <ul> <li v-for="(todo, index) in todos" :key="todo.id"> {{ todo.text }} <button @click="deleteTodo(index)">删除</button> </li> </ul> <form @submit.prevent="addTodo"> <input type="text" v-model="newTodoText" placeholder="添新的待办事项"> <button type="submit">添</button> </form> </div> </template> <script> import { mapGetters, mapActions } from 'vuex' import { getTodos } from '../services/todoService' export default { computed: { ...mapGetters([ 'todos' ]) }, data() { return { newTodoText: '' } }, methods: { ...mapActions([ 'addTodo', 'deleteTodo' ]), fetchTodos() { getTodos() .then(todos => { this.$store.commit('setTodos', todos) }) } }, created() { this.fetchTodos() } } </script> ``` 这里新增了 fetchTodos 方法,使用了 getTodos 方法获取待办事项列表,并将列表提交到 Vuex Store 中。在组件的 created 生命周期中调用 fetchTodos 方法,实现页面初始载时获取待办事项列表的功能。 7. 完成 至此,我们已经完成了一个简单的增删改查功能的待办事项列表应用。当用户打开应用时,页面会自动获取待办事项列表并展示出来。用户可以添新的待办事项、删除已有的待办事项,并实时更新页面展示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值