欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

vue 路由和状态管理

程序员文章站 2022-03-13 13:45:34
...

vue路由

  • 重新安装vue项目

vue create demox
Manually select features

vue 路由和状态管理

  • 认识路由(Vue Router)

vue 路由和状态管理

  • Vue-Router
  1. App.vue
  2. <template>
  3. <div id="nav">
  4. <router-link to="/">Home</router-link> |
  5. <router-link to="/about">About</router-link> |
  6. <router-link to="/user">User</router-link> |
  7. <router-link to="/page" custom v-slot="{ navigate }">
  8. <button @click="navigate" @keypress.enter="navigate" role="link">page</button>
  9. </router-link>
  10. |
  11. <button @click="$router.go(-1)">返回</button>
  12. |
  13. {{$route.path}}
  14. <br>
  15. <a :class="{active:$route.path='/msg'}" href="/msg"> Msg</a>
  16. <button @click="$router.push('/page')">all</button>
  17. <button @click="topage('user')">user</button>
  18. </div>
  19. <router-view/>
  20. </template>
  21. <script>
  22. export default {
  23. name:'App',
  24. data(){
  25. return{
  26. msg:'aaa'
  27. }
  28. },
  29. methods:{
  30. one(){
  31. console.log(this.msg);
  32. // this.$route.path;
  33. // this.$router.
  34. },
  35. topage(path){
  36. this.$router.push(path)
  37. }
  38. }
  39. }
  40. </script>
  41. <style lang="scss">
  42. #app {
  43. font-family: Avenir, Helvetica, Arial, sans-serif;
  44. -webkit-font-smoothing: antialiased;
  45. -moz-osx-font-smoothing: grayscale;
  46. text-align: center;
  47. color: #2c3e50;
  48. }
  49. #nav {
  50. padding: 30px;
  51. a {
  52. font-weight: bold;
  53. color: #2c3e50;
  54. &.router-link-exact-active {
  55. color: #42b983;
  56. }
  57. }
  58. a.active{
  59. color: brown;
  60. }
  61. }
  62. </style>
  63. Page.vue
  64. <template>
  65. <div>
  66. 文章ID:{{$route.params.id}}
  67. </div>
  68. </template>
  69. <script>
  70. export default {
  71. name: "Page",
  72. methods:{
  73. one(){
  74. console.log(this.$route.params.id);
  75. }
  76. }
  77. }
  78. </script>
  79. <style scoped>
  80. </style>
  81. User.vue
  82. <template>
  83. <div>
  84. <h1>会员中心</h1>
  85. <div class="menu">
  86. <li><router-link to="/user/setting">Setting</router-link></li>
  87. <li><router-link to="/user/info">Info</router-link></li>
  88. <li>
  89. <button @click="topage">数据</button>
  90. </li>
  91. </div>
  92. <div class="main">
  93. <router-view/>
  94. </div>
  95. </div>
  96. </template>
  97. <script>
  98. export default {
  99. name: "User",
  100. methods:{
  101. topage(){
  102. this.$router.push({path:'/user/info', query: {name:'zhangsan',age:30}})
  103. }
  104. }
  105. }
  106. </script>
  107. <style scoped lang="scss">
  108. .menu{
  109. width: 30%;
  110. float: left;
  111. height: 300px;
  112. background: #42b983;
  113. a {
  114. font-weight: bold;
  115. color: #2c3e50;
  116. &.router-link-exact-active {
  117. color: red;
  118. }
  119. }
  120. }
  121. .main{
  122. width: 65%;
  123. height: 300px;
  124. float: right;
  125. background: antiquewhite;
  126. }
  127. </style>
  128. Info.vue
  129. <template>
  130. <div>
  131. 个人信息
  132. <h3>
  133. {{$route.query.name}}<br>
  134. {{$route.query.age}}
  135. </h3>
  136. </div>
  137. </template>
  138. <script>
  139. export default {
  140. name: "Info"
  141. }
  142. </script>
  143. <style scoped>
  144. </style>
  145. router/index.js
  146. // import { createRouter, createWebHistory } from 'vue-router'
  147. // import Home from '../views/Home.vue'
  148. // import Page from '../views/Page.vue'
  149. // import User from '../views/User.vue'
  150. import { createRouter, createWebHistory } from 'vue-router'
  151. const Home = () => import( '../views/Home.vue')
  152. const About = () => import( '../views/About.vue')
  153. const Page = () => import( '../views/Page.vue')
  154. const User = () => import( '../views/User.vue')
  155. const Msg = () => import( '../views/Msg.vue')
  156. const Setting = () => import( '../views/Setting.vue')
  157. const Info = () => import( '../views/Info.vue')
  158. const routes = [
  159. {
  160. path: '/',
  161. name: 'Home',
  162. component: Home
  163. },
  164. {
  165. path: '/about',
  166. name: 'About',
  167. // route level code-splitting
  168. // this generates a separate chunk (about.[hash].js) for this route
  169. // which is lazy-loaded when the route is visited.
  170. component:About
  171. },
  172. {
  173. path: '/page/:id',
  174. name: 'Page',
  175. component:Page,
  176. },
  177. {
  178. path: '/user',
  179. name: 'User',
  180. component:User,
  181. //子路由
  182. children:[
  183. {
  184. path: 'setting',
  185. name: 'Setting',
  186. component:Setting
  187. },
  188. {
  189. path: 'info',
  190. name: 'Info',
  191. component:Info
  192. }
  193. ]
  194. },
  195. {
  196. path: '/msg',
  197. name: 'Msg',
  198. component:Msg
  199. }
  200. ]
  201. const router = createRouter({
  202. history: createWebHistory(process.env.BASE_URL),
  203. routes
  204. })
  205. router.beforeEach((to, from )=>{
  206. document.title = to.meta.title;
  207. })
  208. router.afterEach((to, from)=>{
  209. console.log("to---"+to.fullPath);
  210. console.log("from---"+from.fullPath);
  211. })
  212. export default router

vue 路由和状态管理

  • Vuex 状态管理

使用APP全局属性: $store 中的 state

  1. <h3>单组件中应用</h3>
  2. <h2>{{count}}</h2>
  3. <button @click="count--">--</button>
  4. <button @click="count++">++</button>
  5. <h3>使用vuex中的状态</h3>
  6. <h2>{{$store.state.num}}</h2>
  7. <button @click="$store.state.num-=2">-2</button>
  8. <button @click="$store.state.num+=2">+2</button>

通过Mutations修改

  1. state: {
  2. num:10,
  3. dnum:9
  4. },
  5. mutations: {
  6. sub(state,xnum){
  7. state.dnum -=xnum;
  8. },
  9. add(state,xnum){
  10. state.dnum +=xnum;
  11. }
  12. },
  13. methods:{
  14. sub11(xnum){
  15. this.$store.commit('sub',xnum);
  16. },
  17. add11(xnum){
  18. this.$store.commit('add',xnum);
  19. }
  20. }
  21. <h3>通过mutations 修改数据</h3>
  22. <h2>{{$store.state.dnum}}</h2>
  23. <button @click="sub11(5)">-</button>
  24. <button @click="add11(5)">+</button>

计算属性getter应用

  1. getters:{
  2. vxdnumpod(state){
  3. return state.dnum*state.dnum;
  4. }
  5. },
  6. <h2>{{$store.getters.vxdnumpod}}</h2>
  7. <button @click="sub11(5)">-</button>
  8. <button @click="add11(5)">+</button>

Actions异步处理操作

  1. asetnum(state){
  2. state.dnum=12345
  3. }
  4. setnum({state, commit, getters}){
  5. // setnum(context){
  6. // const {state, commit, getters}=context
  7. setTimeout(()=>{
  8. commit('asetnum');
  9. },3000)
  10. }
  11. setdnum(){
  12. this.$store.dispatch('setnum')
  13. }
  14. {{setdnum()}}
  15. <h2>{{$store.state.dnum}}</h2>