位置: IT常识 - 正文

我的VUE 学习之路(下)(vuexy)

编辑:rootadmin
我的VUE 学习之路(下)

推荐整理分享我的VUE 学习之路(下)(vuexy),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue myui,vuexy,vuexy,vue myui,vuexy,vuexl,vue教学文档,vue myui,内容如对您有帮助,希望把文章链接给更多的朋友!

前言:

        在经历过前面在HTML下的VUE相关基础的洗礼后,我们可以动手去做一些事了,此时发现直接通过直接VUE组件方式与之前在HTML不同,首先要“静一静”,细看之下只是对之前的很多写法做了封装。

        本文旨在直接上手Vue项目下做测试而非前面的那种方式。

预期目标:VUE项目开发的基本理解

使用工具:HBuilder

学习前技术储备:VUE 在HTML下的使用

零、准备

下载与配置Vue CLI

注意事项:

创建项目时如果提示:无法加载文件 vue.ps1.......

解决方案:

1.以管理员身份打开windows PowerShell 

2.输入:set-ExecutionPolicy RemoteSigned 

3.选择y或者a

4.回车 就可以了

一、Vue 项目走步

1.1.项目创建

 1.在对应的目录上支行CMD或终端

2.输入 vue create XXX                                        [xxx 是项目名称]

3.选择配置(如下图)通常情况下需要手动配置

 4.手动配置 选项说明

        4.1Babel (默认选中)(选中)

        4.2 TypeScript  --TS库 

        4.3 Progressive Web App...

        4.4 Router        --路由组件(建议选中)

        4.5 Vuex           --状态组件(建议选中)

        4.6 CSS Pre-processors  --样式管理(建议选中)

        4.7 Linter / Formatter(默认选中)(个人不推荐 稍有错误就会提示错误,影响开发速度)

        4.8 Unit Testing  --测试

        4.9 E2E Testing  --测试

5.创建就慢慢等

6.创建后可以使用WebStrom 、VScode 、Hbuilder以项目的打开(导入)就可以了

我的VUE 学习之路(下)(vuexy)

1.2.项目结构

1.3.项目文件

 系统处理顺序个人理解是:public/index.html  -> main.js->App.vue(如果不对请留言指正)

1.4.项目示例

HomeView.vue 文件调用模组Demo.vue

<template> <div class="home"> <img alt="Vue logo" src="../assets/logo.png"><!-- <HelloWorld msg="Welcome to Your Vue.js App"/>--> <demo></demo> </div></template><script>// @ is an alias to /src// import HelloWorld from '@/components/HelloWorld.vue'import Demo from '@/components/Demo'export default { name: 'HomeView', components: { // HelloWorld, Demo }}</script><template> <div> {{msg}} <input type="text" ref="mytest"> <button @click="test()">测试</button> <ul> <li v-for="data in inputData" :key="data">{{data}}</li> </ul> </div></template><script>// 接口export default { // eslint-disable-next-line vue/multi-word-component-names name: 'Demo', // 数据层 data () { return { msg: '这是一个Demo', inputData: [] } }, // 事件 methods: { test () { // eslint-disable-next-line no-sequences,no-unused-expressions console.log(this.$refs.mytest.value), this.inputData.push(this.$refs.mytest.value) } }}</script>二、反向代理概念

它存在的意义在于解决跨域问题,主要是通过修改vue.config.js 处理。

(ps:修改后要重新启动一下才生效)

const { defineConfig } = require('@vue/cli-service')// 示例解决 http://t.163.move/getMove/getById?10module.exports = defineConfig({ transpileDependencies: true, devServer: { proxy: { '/getMove': { // API标识 target: 'http://t.163.move', // 代理标识 // wss: true, changeOrigin: true } } }})// 调用 axios.get("/getMove/getById?10").then(res=>{res.data})三、路由

        路由顾名思意就是负责转发或跳转的,与我们生活中的路由器的工作方式相同;在下载的文件中包含了一个示例(router\index.js),所有接受路由的地方需要用<router-view></router-view>容器接收。

 一级路由

        其实VUE在创建时就给示出一个一级路由实例;

        App.vue

<template> <div id="app"> <nav> <router-link to="/">Home</router-link> | <router-link to="/about">About</router-link> | </nav> <router-view/> </div></template><style lang="scss">#app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50;}nav { padding: 30px; a { font-weight: bold; color: #2c3e50; &.router-link-exact-active { color: #42b983; } }}</style>

       router\index.js

import Vue from 'vue'import VueRouter from 'vue-router'import HomeView from '../views/HomeView.vue'Vue.use(VueRouter)const routes = [ { path: '/',//访问路径 name: 'home',//命名 component: HomeView //外部注册 这里直接使用 }, { path: '/about', name: 'about', component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')//内部引用 }]const router = new VueRouter({ mode: 'history',//路由模式 展示方式 base: process.env.BASE_URL, routes})export default router 多级路由

作用:用于访问子路径  通常使用在二级目录配置

Demo.vue 模拟父(注意定义对应接收标签)

<template> <div> 这是一个Demo 为的是展示二级(多级)路由 <!-- 设置好容器--> <router-view></router-view> </div></template>

定义两个子组件 A.vue  与B.vue

<template> <div> A </div></template><template> <div>B</div></template>

配置路由新增

{ path: '/demo', name: 'demo', component: Demo, children:[ { path:'a', name:'a', component:A }, { path:'b', name:'b', component:B } ] }动态路由

用途:主要用于路径是变化,但格式固定,通常使用在列表中的详情页

ProductDetail.vue 模拟产品详情

<template> <div> 这是一个组件 <div> {{id}}</div> </div></template><script>export default { data(){ return{ id:"" } }, mounted(){ this.id=this.$route.params.id }}</script>

AboutView.vue 模拟数据(产品)列表页,用于发出明细请求

<template> <div class="about"> <h1>This is an about page</h1> <ul> <li v-for="data in list" :key="data.id" @click="theProduct(data.id)"> {{data}} </li> </ul> </div></template><script>export default { data(){ return{ list:[{id:1,code:"aa"},{id:2,code:"bb"},{id:3,code:"cc"}] } }, methods:{ theProduct(id){ this.$router.push(`/detail/${id}`)//指向router这个目录下的配置实现跳转 动态路由方式1 //this.$router.push({name:"detail",params:{id:id}})//动态路由方式2 通过命名路由方式跳转 } }}</script>

路由配置

import Vue from 'vue'import VueRouter from 'vue-router'import HomeView from '../views/HomeView.vue'import ProductDetail from '../components/ProductDetail'Vue.use(VueRouter)const routes = [ { path: '/',//访问路径 name: 'home',//命名 component: HomeView //外部注册 这里直接使用 }, { path: '/about', name: 'about', component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')//内部引用 }, { path:'/detail/:id', name:'detail', component: ProductDetail }]const router = new VueRouter({ mode: 'history',//路由模式 展示方式 base: process.env.BASE_URL, routes})export default router 路由拦截

作用:路由前的校验,一般用于登陆等

如下代码在路由js中添加进去代表当不是demo路径的访问就放行

//路由拦截配置 router.beforeEach((to,from,next) =>{ if (to.name !=='demo'){ next() } })路由定向

作用:当访问未在路由内部配置的路径时要自动指向指定的路径

如下配置当访问 .../demo/c的时候自动转向.../demo这个路径上

{ path: '/demo', name: 'demo', component: Demo, children:[ { path:'a', name:'a', component:A }, { path:'b', name:'b', component:B }, { //重定向 path: '*', redirect:'/demo' } ] }

全部示例代码

components\productDetail.vue

<template> <div> 这是一个组件 <div> {{id}}</div> </div></template><script>export default { data(){ return{ id:"" } }, mounted(){ this.id=this.$route.params.id }}</script>

router\index.js

import Vue from 'vue'import VueRouter from 'vue-router'import HomeView from '../views/HomeView.vue'import ProductDetail from '../components/ProductDetail'import Demo from '../views/Demo'import A from '../views/Demo/A'import B from '../views/Demo/B'Vue.use(VueRouter)const routes = [ { path: '/',//访问路径 name: 'home',//命名 component: HomeView //外部注册 这里直接使用 }, { path: '/about', name: 'about', component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')//内部引用 }, { path:'/detail/:id', name:'detail', component: ProductDetail }, { path: '/demo', name: 'demo', component: Demo, children:[ { path:'a', name:'a', component:A }, { path:'b', name:'b', component:B }, { //重定向 path: '*', redirect:'/demo' } ] }, //重定向:在顶级目录中不存在的数据自动指向首页 { path: '*', redirect:'/' }]const router = new VueRouter({ mode: 'history',//路由模式 展示方式 base: process.env.BASE_URL, routes})//路由拦截配置// router.beforeEach((to,from,next) =>{// if (to.name !=='demo'){// next()// }// })export default router

views\AboutView.vue

<template> <div class="about"> <h1>This is an about page</h1> <ul> <li v-for="data in list" :key="data.id" @click="theProduct(data.id)"> {{data}} </li> </ul> </div></template><script>export default { data(){ return{ list:[{id:1,code:"aa"},{id:2,code:"bb"},{id:3,code:"cc"}] } }, methods:{ theProduct(id){ this.$router.push(`/detail/${id}`)//指向router这个目录下的配置实现跳转 动态路由方式1 //this.$router.push({name:"detail",params:{id:id}})//动态路由方式2 通过命名路由方式跳转 } }}</script>

views\Demo.vue

<template> <div> 这是一个Demo 为的是展示二级(多级)路由 <!-- 设置好容器--> <router-view></router-view> </div></template>

views\Demo\A.vue

<template> <div> A </div></template>

views\Demo\B.vue

<template> <div>B</div></template>

App.vue

<template> <div id="app"> <nav> <router-link to="/">Home</router-link> | <router-link to="/about">About</router-link> | <router-link to="/detail">detail</router-link> </nav> <router-view/> </div></template><style lang="scss">#app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50;}nav { padding: 30px; a { font-weight: bold; color: #2c3e50; &.router-link-exact-active { color: #42b983; } }}</style>四、Vuex

vuex 是Vue家族中的状态管理工具。PC端常用于权限管理,移动端则多数用于懒加载。其配置文件前文已介绍就是store目录下的index.js文件。请大家看我针对这个文件的注释

import Vue from 'vue'//引用vueimport Vuex from 'vuex'//引用状态管理import axios from 'axios'Vue.use(Vuex) //加载//导出export default new Vuex.Store({ state: { //---------------作用:共享数据 第三个加载 productList:[] }, getters: { //---------------作用:数据二次加工方法 getProductListTop3(state){ return state.productList.filter((item,index)=>index<4) } }, mutations: { //---------------作用:配置系统常量比如登陆的账户信息、权限信息等 第二个加载 setProductList(state,data){ state.productList=data } }, actions: { //---------------作用:异步加载 条件执行,常用于懒加载 第一个加载 getProductList(store){ axios({ url:"http://127.0.0.1:2022/company-work-time/findByCompanyId?companyId=74" }).then(res=>{ //state.productList=res.data; store.commit("setProductList",res.data.data) console.log(res.data.data) }) } }, modules: { //---------------状态树,暂时用不到,一般较大项目使用,相当于
本文链接地址:https://www.jiuchutong.com/zhishi/299795.html 转载请保留说明!

上一篇:ChatGPT-4.0 : 未来已来,你来不来(xch 未来)

下一篇:【NLP屠夫系列】- NER之实战BILSTM(网络用语屠夫)

  • 退税怎么做账务处理,需要交什么税
  • 增值税附加税怎么填
  • 人力资源公司劳务外包
  • 其他应收款是负数怎么报税
  • 增值税包括哪些内容
  • 进项税额转出税率
  • 发票旁边的孔能撕吗
  • 知道含税价,税率,怎么算税金
  • 转登记小规模纳税人留抵税额
  • 企业购买国债逆回购需要缴纳增值税吗
  • 小规模建筑企业预缴税款
  • 免税企业取得增值税专用发票怎么处理
  • 企业所得税一般纳税人是怎么缴纳的
  • 创可贴属于什么费用科目
  • 专票税率开错了有什么影响
  • 用于集体福利的进项税额转出
  • 电梯维护费怎么做账
  • 中央非税收入统一票据怎么做账
  • 生产企业原材料的订购与运输建模
  • 六月工资七月发算不算压工资
  • 小规模减半征收什么时候开始
  • 小微企业关闭
  • 工资薪金所得税前扣除项目标准
  • mac桌面怎么整理
  • 华为mate50耳机孔和充电口一样吗
  • 设立全资子公司的风险
  • 开启开发人员模式的方法
  • win11预览版怎么变回正式版
  • PHP:iterator_count()的用法_spl函数
  • 企业的支付宝
  • 增值税专票跨月但未认证,怎么作废
  • 固定资产的使用寿命超过一个会计年度
  • less预处理器详解
  • vue qrcode生成二维码
  • uniapp 手写识别
  • 毕业设计基于51单片机多功能电子钟的设计
  • vue文件下载功能
  • 支付中间人佣金违法吗
  • 法人股东分红要缴纳多少税
  • 个体经营所得可以用投资人申报吗
  • 远期汇票分为哪几种
  • 房企预缴增值税
  • 吸收合并需要清算嘛
  • 科技型中小企业享受优惠税收政策
  • 一般纳税人应交增值税
  • 揭秘如何投屏纽约时代广场
  • sql无法运行
  • 电子税务局是指什么意思
  • 库存商品结转成本的金额怎么计算
  • 行程单入账多久钱能到账
  • 留存收益转增资本公积
  • 个体户超额
  • 收到投资款现金流量放到哪里
  • mysql导入导出sql文件
  • sql数据库连不上可能的原因
  • Sql Server datetime问题
  • mysql5.7.31安装配置教程
  • windows取消ctrl+alt+del
  • linux日志类型
  • 国产操作系统有免费的吗
  • task运行在哪个工作单元
  • hosts文件位置在哪
  • 360误删文件恢复怎么恢复
  • linux中遇到的问题
  • linux shell怎么用
  • windows 8
  • windows8截屏
  • 水下摄影技巧
  • jquery table加载数据
  • cocos2dx创建项目
  • 浅析科学发展观的核心立场
  • 用jquery
  • javascriptj
  • jquery中的css方法
  • python multinomial
  • JavaScript的removeChild()函数用法详解
  • javascript面向对象精要pdf
  • javascript巧用eval函数组装表单输入项为json对象的方法
  • 网上交购置税怎么拿完税证明
  • 税票电话号码变更影响抵扣吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设