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

  • 一般纳税人哪些可以开3%的发票吗
  • 计提所得税费用会计分录
  • 实收资本的期末余额在借方还是贷方
  • 受托加工的物资是哪个科目
  • 汇款退回来的可以不做账吗
  • 领增值税专票需要发票吗
  • 企业所得税资产总额怎么填写
  • 资产盘点的步骤是什么,需要注意哪些细节
  • 公司装修费可以计入开办费吗
  • 小规模增值税征收条件
  • 租房的装修费怎么入账
  • 劳保费计入管理费用的什么
  • 转让无形资产收入属于销售收入吗
  • 一般纳税人从小规模纳税人取得专用发票
  • 纳税人转让2016年以后的土地使用权
  • 挂账的费用怎么填制单据凭证
  • 公司代扣的社保怎么做分录
  • 旅游业开办费
  • 托收承付是什么意思?
  • 发票上月开本月到账要交企业所得税吗?
  • 视同内销发票重开要带税吗
  • 单位收到社保补贴会计怎样处理
  • 一般劳务报酬和其他劳务报酬的区别
  • linux用途是什么
  • 对违规送礼行为怎么处理
  • 酒店购进布草可直接确认为成本吗
  • PHP:JDToJulian()的用法_日历函数
  • laravel登录接口
  • 微信小程序登录后端
  • laravel怎么用
  • react生命周期分为两类
  • 深入node.js技术栈
  • 论文导语如何写
  • php文件上传技术
  • upf命令
  • 给分公司开的发票,总公司付的款可以吗
  • 哪些情形视同从境外取得收入:
  • 发票过期未认证可以用吗
  • 帝国主义
  • 批处理命令在windows操作中的典型应用
  • 汇票没到期如何兑现
  • 在业和存续的区别
  • 分期收款发出商品是什么意思
  • 销项负数发票如何作废
  • 外经证预缴税款网上申报
  • 投资子公司现金流量表
  • 公司认缴没有实缴会有什么风险
  • 装修费用不摊销怎么入账
  • 委托加工物资的加工费计入什么科目
  • 事业单位的非税收入账务处理
  • sql搜索字段名
  • centos下安装虚拟机
  • win8.1系统安装教程
  • linux 详解
  • xmpdisabled什么意思
  • 怎么调整桌面图标间距
  • mssecsvr.exe病毒现象
  • 王者自动更新怎么关
  • 怎么把硬盘安装到电脑上
  • win8系统如何备份系统
  • w8系统一键恢复
  • win10 20h2 微软账户
  • python 读中文文件
  • cocos2d::DrawPrimitives和DrawNode分别实现画板功能
  • store与storage的区别
  • python编程数字
  • 用python做开发
  • Drag and drop in unity
  • JavaScript+html5 canvas实现本地截图教程
  • python库怎么用
  • unity打开c#
  • JavaScript中的数据类型分哪为两大类?
  • jquery获取file文件
  • js如何封装
  • js super方法
  • python3 cookie
  • python ntlk
  • 青海国家税务局总局官网
  • 如何查询公司的电话号码
  • 市民服务热线有用吗
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

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

    友情链接: 武汉网站建设