VUE入门教程(vue从入门到实战)
bigegpt 2024-11-16 17:11 5 浏览
vue-cli是官方提供的一个脚手架,用于快速生成一vue项目,有点类似java中使用maven构建项目
需要环境
Node.js : http://nodejs.cn/download/ 安装完后在Windows的cmd窗口输入 node -v及npm -v 如果有版本号,那么说明安装成功 也可以安装淘宝的镜像,这样下载的话会快很多,安装淘宝镜像后可以使用cnpm指令
# -g 全局安装
npm install cnpm -g
npm config set registry https://registry.npm.taobao.org
npm install cnpm -g
安装位置:C:\Users\Administrator\AppData\Roaming\npm
安装vue-cli
#在命令台输入
cnpm install vue-cli -g
#查看是否安装成功
vue list
创建第一个vue-cli程序
1、在本地磁盘创建一个空文件夹用来存放项目 D:\vue\vuenote 2、使用控制台在该目录下执行创建vue应用程序指令
D:\vue\vuenote>vue init webpack first-vue
3、一路选择no 4、进入项目目录,安装依赖
D:\vue\vuenote>cd first-vueD:\vue\vuenote\first-vue>cnpm install
5、启动项目
npm run dev
打开浏览器输入 http://localhost:8080/
webpack
webpack是一个现代JavaScript应用程序的静态模块打包器(module bundler)。当webpack处理应用程序时,它会递归地构建一个依赖关系图(dependency graph),其中包含应用程序需要的每个模块,然后将所有这些模块打包成一个或多个bundle.
webpack的使用
1、在本地磁盘上创建一个空目录,并使用idea打开
2、按如下结构创建目录和文件
3、在hello.js暴露一个sayhai的方法
exports.sayHai=function () { document.write("<h1>hello world</h1>")}
4、在main.js导入该方法
var hello=require('./hello')hello.sayHai()
5、在webpack.config.js中配置打包
module.exports={ entry:'./modules/main.js', output:{ filename:'./js/bundle.js' }}
6、在idea控制台运行 webpack指令 运行webpack指令后,会在当前项目的生成dist/js/bundle.js 7、在index.html中引入bundle.js文件
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title></head><body> <script src="dist/js/bundle.js"></script></body></html>
Vue-Router路由
Vue Router是Vue.js官方的路由管理器(路径跳转)。它和Vue.js的核心深度集成,让构建单页面应用变得易如反掌。
安装路由
使用idea在当前项目的控制台上输入指令
cnpm install vue-router --save-dev
路由的使用
1、在component目录下创建一个vue组件Content.vue
import Vue from 'vue'
import VueRouter from 'vue-router'
import Content from "../components/Content";
//安装路由
Vue.use(VueRouter);
export default new VueRouter({
routes:
[
{
//路由路径
path: '/content',
name: 'content',
//跳转的组件
component: Content
}
]
})
2、在当前项目下创建router目录,router目录下创建用来配置路由的配置文件index.js index.js内容如下:
import Vue from 'vue'
import VueRouter from 'vue-router'
import Content from "../components/Content";
//安装路由
Vue.use(VueRouter);
export default new VueRouter({
routes:
[
{
//路由路径
path: '/content',
name: 'content',
//跳转的组件
component: Content
}
]
})
3、在App.vue中配置请求路由
<template>
<div id="app">
<img src="./assets/logo.png">
//请求路由
<router-link to="/content">内容页</router-link>
//路由结果在此处展示
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
components: {
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
vue+elementUI
vue配合elementUI可以使我们的页面更加美观
elementUI的使用
1、创建一个新的vue项目
vue init webpack vue_element
2、安装插件(vue-router、element-ui、sass-loader、node-sass)
# 进入工程目录
cd vue_element
# 安装 vue-router
npm install vue-router --save-dev
# 安装 element-ui
npm i element-ui -S
# 安装依赖
npm install
# 安装 SASS 加载器
cnpm install sass-loader node-sass --save-dev
# 启动测试
npm run dev
3、创建一个Login.vue组件,内容如下:
<template>
<div>
<el-form ref="loginForm" :model="form" :rules="rules" label-width="80px" class="login-box">
<h3 class="login-title">欢迎登录</h3>
<el-form-item label="账号" prop="username">
<el-input type="text" placeholder="请输入账号" v-model="form.username"/>
</el-form-item>
<el-form-item label="密码" prop="password">
<el-input type="password" placeholder="请输入密码" v-model="form.password"/>
</el-form-item>
<el-form-item>
<el-button type="primary" v-on:click="onSubmit('loginForm')">登录</el-button>
</el-form-item>
</el-form>
<el-dialog
title="温馨提示"
:visible.sync="dialogVisible"
width="30%"
:before-close="handleClose">
<span>请输入账号和密码</span>
<span slot="footer" class="dialog-footer">
<el-button type="primary" @click="dialogVisible = false">确 定</el-button>
</span>
</el-dialog>
</div>
</template>
<script>
export default {
name: "Login",
data() {
return {
form: {
username: '',
password: ''
},
// 表单验证,需要在 el-form-item 元素中增加 prop 属性
rules: {
username: [
{required: true, message: '账号不可为空', trigger: 'blur'}
],
password: [
{required: true, message: '密码不可为空', trigger: 'blur'}
]
},
// 对话框显示和隐藏
dialogVisible: false
}
},
methods: {
onSubmit(formName) {
// 为表单绑定验证功能
this.$refs[formName].validate((valid) => {
if (valid) {
// 使用 vue-router 路由到指定页面,该方式称之为编程式导航
this.$router.push("/main");
} else {
this.dialogVisible = true;
return false;
}
});
}
}
}
</script>
<style lang="scss" scoped>
.login-box {
border: 1px solid #DCDFE6;
width: 350px;
margin: 180px auto;
padding: 35px 35px 15px 35px;
border-radius: 5px;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
box-shadow: 0 0 25px #909399;
}
.login-title {
text-align: center;
margin: 0 auto 40px auto;
color: #303133;
}
</style>
4、配置路由
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from "../components/Login";
//安装路由
Vue.use(VueRouter)
export default new VueRouter({
routes:[
{
path:'/login',
name:'login',
component:Login
}
]
})
5、在main.js引入路由和elementUI
// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
//导入elementUI
import ElementUI from "element-ui"
//导入element css
import 'element-ui/lib/theme-chalk/index.css'
Vue.config.productionTip = false
Vue.use(router);
Vue.use(ElementUI)
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
render: h => h(App),//ElementUI规定这样使用
})
6、在App.vue中请求路由
<template>
<div id="app">
<router-link to="/login">登录</router-link>
<router-view></router-view>
</div>
</template>
<script>
export default {
name: 'App',
components: {
}
}
</script>
<style>
#app {
font-family: 'Avenir', Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
7、测试 npm run dev
注意:如果项目运行失败,可以在package.json里降低sass-loader和node-sass的版本
"sass-loader": "^7.3.1",
"node-sass": "^4.9.0",
嵌套路由
简单说就是在路由里再套一个子路由
1、创建一个作为子路由Profile.vue组件
<template>
<h1>用户列表</h1>
</template>
<script>
export default {
name: "List"
}
</script>
<style scoped>
</style>
2、Main.vue里请求路由
<template>
<div>
<el-container>
<el-aside width="200px">
<el-menu :default-openeds="['1']">
<el-submenu index="1">
<template slot="title"><i class="el-icon-caret-right"></i>用户管理</template>
<el-menu-item-group>
<el-menu-item index="1-1">
<!--插入的地方-->
<router-link to="/user/profile">个人信息</router-link>
</el-menu-item>
<el-menu-item index="1-2">
<!--插入的地方-->
<router-link to="/user/list">用户列表</router-link>
</el-menu-item>
</el-menu-item-group>
</el-submenu>
<el-submenu index="2">
<template slot="title"><i class="el-icon-caret-right"></i>内容管理</template>
<el-menu-item-group>
<el-menu-item index="2-1">分类管理</el-menu-item>
<el-menu-item index="2-2">内容列表</el-menu-item>
</el-menu-item-group>
</el-submenu>
</el-menu>
</el-aside>
<el-container>
<el-header style="text-align: right; font-size: 12px">
<el-dropdown>
<i class="el-icon-setting" style="margin-right: 15px"></i>
<el-dropdown-menu slot="dropdown">
<el-dropdown-item>个人信息</el-dropdown-item>
<el-dropdown-item>退出登录</el-dropdown-item>
</el-dropdown-menu>
</el-dropdown>
</el-header>
<el-main>
<!--在这里展示视图-->
<router-view />
</el-main>
</el-container>
</el-container>
</div>
</template>
<script>
export default {
name: "Main"
}
</script>
<style scoped lang="scss">
.el-header {
background-color: #B3C0D1;
color: #333;
line-height: 60px;
}
.el-aside {
color: #333;
}
</style>
3、测试
参数传递
参数传递过程:url请求路径—->路由接收参数—->跳转套组件显示参数
1、url请求路径
<router-link :to="{name:'Profile',params:{id:1} }">个人信息</router-link>
2、路由接收参数
方式一:
{path:'/user/profile/:id',name:'Profile',component:Profile},
方式二:
{path:'/user/profile/:id',name:'Profile',component:Profile,props:true}
3、组件模板展示参数
方式一:
{ {$route.params.id} }
方式二:
<template>
<div>
{ {id} }
</div>
</template>
<script>
export default {
//接收路由传过来的id
props:['id'],
name: "Profile"
}
</script>
<style scoped>
</style>
路由钩子与异步请求
路由模式
hash:路径带 # 符号(默认),如 http://localhost/#/login history:路径不带 # 符号,如 http://localhost/login
路由钩子与异步请求
beforeRouteEnter:在进入路由前执行 beforeRouteLeave:在离开路由前执行 类似于过滤器,在进入模板前可以使用路由钩子进行异步请求数据,并在模板展示
<template>
<div>
{ {info.url} }
</div>
</template>
<script>
export default {
props:['id'],
name: "Profile",
beforeRouteEnter:(to,from,next)=>{
console.log("进入页面之前");
next(vm=>{
//进入路由之前执行getData方法
vm.getData()
});
},
beforeRouteLeave:(to,from,next)=>{
console.log("离开页面之前")
next();
},
//返回请求的数据
data(){
return{
info:{
}
}
},
methods:{
getData:function () {
//使用axios异步请求数据
this.axios({
method:'get',
url:'http://localhost:8080/static/mock/data.json'
}).then(res=>(this.info=res.data))
}
}
}
</script>
<style scoped>
</style>
相关推荐
- 悠悠万事,吃饭为大(悠悠万事吃饭为大,什么意思)
-
新媒体编辑:杜岷赵蕾初审:程秀娟审核:汤小俊审签:周星...
- 高铁扒门事件升级版!婚宴上‘冲喜’老人团:我们抢的是社会资源
-
凌晨两点改方案时,突然收到婚庆团队发来的视频——胶东某酒店宴会厅,三个穿大红棉袄的中年妇女跟敢死队似的往前冲,眼瞅着就要扑到新娘的高额钻石项链上。要不是门口小伙及时阻拦,这婚礼造型团队熬了三个月的方案...
- 微服务架构实战:商家管理后台与sso设计,SSO客户端设计
-
SSO客户端设计下面通过模块merchant-security对SSO客户端安全认证部分的实现进行封装,以便各个接入SSO的客户端应用进行引用。安全认证的项目管理配置SSO客户端安全认证的项目管理使...
- 还在为 Spring Boot 配置类加载机制困惑?一文为你彻底解惑
-
在当今微服务架构盛行、项目复杂度不断攀升的开发环境下,SpringBoot作为Java后端开发的主流框架,无疑是我们手中的得力武器。然而,当我们在享受其自动配置带来的便捷时,是否曾被配置类加载...
- Seata源码—6.Seata AT模式的数据源代理二
-
大纲1.Seata的Resource资源接口源码2.Seata数据源连接池代理的实现源码3.Client向Server发起注册RM的源码4.Client向Server注册RM时的交互源码5.数据源连接...
- 30分钟了解K8S(30分钟了解微积分)
-
微服务演进方向o面向分布式设计(Distribution):容器、微服务、API驱动的开发;o面向配置设计(Configuration):一个镜像,多个环境配置;o面向韧性设计(Resista...
- SpringBoot条件化配置(@Conditional)全面解析与实战指南
-
一、条件化配置基础概念1.1什么是条件化配置条件化配置是Spring框架提供的一种基于特定条件来决定是否注册Bean或加载配置的机制。在SpringBoot中,这一机制通过@Conditional...
- 一招解决所有依赖冲突(克服依赖)
-
背景介绍最近遇到了这样一个问题,我们有一个jar包common-tool,作为基础工具包,被各个项目在引用。突然某一天发现日志很多报错。一看是NoSuchMethodError,意思是Dis...
- 你读过Mybatis的源码?说说它用到了几种设计模式
-
学习设计模式时,很多人都有类似的困扰——明明概念背得滚瓜烂熟,一到写代码就完全想不起来怎么用。就像学了一堆游泳技巧,却从没下过水实践,很难真正掌握。其实理解一个知识点,就像看立体模型,单角度观察总...
- golang对接阿里云私有Bucket上传图片、授权访问图片
-
1、为什么要设置私有bucket公共读写:互联网上任何用户都可以对该Bucket内的文件进行访问,并且向该Bucket写入数据。这有可能造成您数据的外泄以及费用激增,若被人恶意写入违法信息还可...
- spring中的资源的加载(spring加载原理)
-
最近在网上看到有人问@ContextConfiguration("classpath:/bean.xml")中除了classpath这种还有其他的写法么,看他的意思是想从本地文件...
- Android资源使用(android资源文件)
-
Android资源管理机制在Android的开发中,需要使用到各式各样的资源,这些资源往往是一些静态资源,比如位图,颜色,布局定义,用户界面使用到的字符串,动画等。这些资源统统放在项目的res/独立子...
- 如何深度理解mybatis?(如何深度理解康乐服务质量管理的5个维度)
-
深度自定义mybatis回顾mybatis的操作的核心步骤编写核心类SqlSessionFacotryBuild进行解析配置文件深度分析解析SqlSessionFacotryBuild干的核心工作编写...
- @Autowired与@Resource原理知识点详解
-
springIOCAOP的不多做赘述了,说下IOC:SpringIOC解决的是对象管理和对象依赖的问题,IOC容器可以理解为一个对象工厂,我们都把该对象交给工厂,工厂管理这些对象的创建以及依赖关系...
- java的redis连接工具篇(java redis client)
-
在Java里,有不少用于连接Redis的工具,下面为你介绍一些主流的工具及其特点:JedisJedis是Redis官方推荐的Java连接工具,它提供了全面的Redis命令支持,且...
- 一周热门
- 最近发表
- 标签列表
-
- mybatiscollection (79)
- mqtt服务器 (88)
- keyerror (78)
- c#map (65)
- resize函数 (64)
- xftp6 (83)
- bt搜索 (75)
- c#var (76)
- mybatis大于等于 (64)
- xcode-select (66)
- mysql授权 (74)
- 下载测试 (70)
- linuxlink (65)
- pythonwget (67)
- androidinclude (65)
- logstashinput (65)
- hadoop端口 (65)
- vue阻止冒泡 (67)
- oracle时间戳转换日期 (64)
- jquery跨域 (68)
- php写入文件 (73)
- kafkatools (66)
- mysql导出数据库 (66)
- jquery鼠标移入移出 (71)
- 取小数点后两位的函数 (73)