位置: 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(网络用语屠夫)

  • 个人所得税人员信息采集验证不通过
  • 税务师报名入口官网2022
  • 委托合同有效吗
  • 企业进口葡萄酒也要缴纳消费税吗
  • 员工外出办事报备制度
  • 红字发票冲红需要收回原发票吗
  • 存货报废需要进项转出吗
  • 一次性奖金并入综合所得
  • 外购商品赠送客户怎么做账
  • 小规模纳税人开专票税率是1%还是3%
  • 固定资产正常报废如何处理
  • 企业受托开发软件是什么
  • 医院其他应付款过高的原因
  • 出售车辆需要缴纳哪些税
  • 水费分割单由哪一方出具
  • 所得税费用是在哪个科目
  • 大连国税局工资待遇怎么样
  • 总包分包差额征税是什么意思
  • 公司付给个人的借款利息怎么做账
  • 申报更正退税增值税申报表如何反应
  • 合同取得成本和增量成本有什么区别
  • 施工企业临时设施属于
  • 增值税专用发票的税率是多少啊
  • 税控盘不交服务费的后果
  • 总公司人员的工资子公司可以发吗
  • phpstudy配置ftp服务器
  • 食品类发票入账属于什么科目
  • 社保挂靠会计处理
  • PHP:session_register_shutdown()的用法_Session函数
  • 哪些情形不属于伪现金
  • 体积最小的u盘
  • php pdo oracle
  • Symfony2实现在controller中获取url的方法
  • 企业其他应付款太多怎么办
  • vue3 script setup withdefault
  • Pytorch深度学习实战3-5:详解计算图与自动微分机(附实例)
  • effective c++ github
  • nmcli命令全称
  • 商贸公司库存怎么盘点准确一点儿
  • 防伪税控开票
  • 小微企业所得税优惠政策最新2023
  • 贴现金额的会计分录
  • 房屋出租收入是其他业务收入吗
  • 创建一个空的学生基本信息表的副本
  • 单位购买的化妆品怎么用
  • 应付职工薪酬包含哪些科目
  • 免税收入不征税收入计入收入总额吗
  • 房地产项目完工清算报告
  • 母公司溢价收购子公司
  • 利息收入缴纳税率怎么算
  • 公司法规定股权转让需要满足什么条件
  • 小微企业免征的增值税属于政府补助吗
  • 以公司名义开的口腔诊所法人和负责人是两个人么
  • 员工报销停车费计入什么科目
  • 收到伙食费的会计处理
  • 税审报告一定要税所主任签吗
  • sql语句参数值
  • windows vista(service pack1)
  • mac购买建议
  • linux系统中文件权限分为哪三种
  • centos 网络监控
  • KunlunPlatform.exe是什么进程?KunlunPlatform.exe是安全的程序吗?
  • 命令提示符操作方法
  • 打开电脑显示配置windows,可是一直0%,怎么办
  • 前端html中怎么让文字左移
  • cocos2dx4.0教程
  • 数组observer
  • android学习路线
  • 网络游戏数据包
  • listview的item
  • jQuery实现表格与ckeckbox的全选与单选功能
  • jquery html5 视频播放控制代码
  • JavaScript中的数据类型
  • diy相册设计
  • 如何用jquery
  • android判断应用是否在前台
  • js如何使用cookie
  • java script js
  • 如何将文件夹导入idea
  • 王军调研地税局的职务
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设