VUE入门教程 vue 入门
zhezhongyun 2024-12-17 17:42 31 浏览
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>
相关推荐
- Angular UI组件库入门指南 - 如何安装和开始使用(一)
-
本文主要介绍如何安装和开始使用KendoUIforAngular。首先完成安装步骤。然后创建一个简单的应用程序,其中包含一些KendoUIforAngular组件,应用程序源代码可供您参考...
- SPSS22: 3.1.3 数据管理—复制数据属性
-
内容摘自《SPSS常用统计分析教程(SPSS22.0中英文版)(第4版)》3.1.3复制数据属性复制数据属性(CopyDataProperties)可用于建立相同调查问卷的空白数据集,或者复制其...
- 妙用Excel制作漂亮工整的工资条(excel表格中如何制作工资条)
-
工资条的制作方法并不难,但如何用Excel把工资条做得更美观、更有效率,可就要花费一点小心思了。许多人或许已经习惯了用工资明细表做员工工资统计并向上级汇报,将表中的条目慢慢复制粘贴然后打印给员工,这样...
- 如何运用EXCEL制作员工工资条,你造吗?
-
每个月工资发放之后,正规的公司应发给每个员工一个工资条。上面有员工当月工资的详细构成。但不能将工资明细表剪条发放,因为每个数字缺少对应项目,这就需要重新制作一张专门用来打印的工资条。作为劳动者,应该妥...
- 鸿蒙仓颉语言开发实战教程:实现商品分类页
-
今天继续为大家带来仓颉语言开发商城应用的实战教程,今天的内容是实现商品分类页。分类页面要在基本布局的基础上增加一些动态效果,比如点击状态的切换和两个列表容器的联动。下面为大家详细介绍。分类列表先来看左...
- 鸿蒙开发实战:一多开发之缩放布局
-
在HarmonyOS中,使用ArkTS语法进行自适应布局时,缩放布局是一种重要的布局方式。它允许组件根据外部容器的尺寸变化,按照预设的比例或权重调整自身的大小,从而确保在不同设备上都能呈现出良好的视觉...
- 基于WPF的电能质量检测系统上位机软件设计
-
郑恒持,蒋丁宇,卢兴泉,刘泊江(大连海事大学轮机工程学院,辽宁大连116026)摘要:电能质量直接影响着电力系统能否安全运行,为了能及时可靠地检测电能质量,采用全新的WindowsPresen...
- HarmonyOS实战:Tab顶部滑动悬停功能实现
-
前言日常开发过程中,遇到这种Scroll嵌套List列表滑动顶部悬停的场景十分常见,在鸿蒙开发时也正好实现了这个功能,本篇文章将带你一步步实现Tab顶部悬停的效果,建议点赞收藏!实现效果先...
- Axure教程:高级搜索(axure搜索功能怎么做)
-
在原型中,搜索是一个常见的交互设计。但不少同学因为技能不熟悉就没有做对应的交互效果。这篇文章,作者分享了设计搜索功能的整个流程,相信看完你也能做一个很牛逼的交互。高级搜索可以通过使用精确的关键词或短语...
- Excel小技巧: 如何设置自动列宽适应内容
-
我们在整理Excel表格的时候,通常会碰到单元格列宽混乱的情况(如下图所示),这会导致数据显示不完整或浪费空间导致打印不全,每次手动调整列宽都会费时费力,下面教你三个方法,让你一键设置自动列宽,适应单...
- 用好6个公式 Excel随意查询(excel中查询功能怎么用公式)
-
Excel表格一般会储存大量数据,我们可能不是每次都需要使用其中的所有数据,大部分时候只用到其中的一部分,所以数据查询功能就变得非常重要。为此,Excel本身也提供了多少查询方法,供我们使用。首先我们...
- 夏日PC消暑指南:机箱风道与风扇选择
-
进入六月以后北京的天气真是热得让人感觉喘不过气,大家天天打交道的笔记本和台式机更是连人都不如了,所谓热成狗真是一点也不夸张。年年大家在防暑抗高温这个问题上都是八仙过海各显神通,但是很多人光顾着自己凉快...
- Excel VBA必学技巧:用厘米设置单元格大小,办公效率翻倍
-
痛点:Excel默认单位太反人类!你是否经常遇到这些问题:-想设置精确的单元格尺寸,却只能用模糊的"字符宽度"和"磅值"?-设计打印报表时,毫米级的误差导致格式错乱...
- CSS小知识,分享14个你可能还未用上但又实用的CSS属性(下)
-
大家好,在上一篇文章里CSS小知识,分享14个你可能还未用上但又实用的CSS属性(上)我们一起学习了上半部分,这篇文章我们我们继续学习下半部分。八、CSSShakeEffect晃动效果CSS...
- 总结雅虎前端性能优化技巧(16条)(雅虎引擎还能用吗)
-
前言在日常开发中,有很多场景需要我们去做好前端优化,为了防止遗忘,加深记忆,今天参阅了一些资料以及自己的一些总结,梳理出来15条优化技巧。1.合并文件css、js合并,减少http请求数,每次http...
- 一周热门
- 最近发表
- 标签列表
-
- HTML 教程 (33)
- HTML 简介 (35)
- HTML 实例/测验 (32)
- HTML 测验 (32)
- JavaScript 和 HTML DOM 参考手册 (32)
- HTML 拓展阅读 (30)
- HTML常用标签 (29)
- HTML文本框样式 (31)
- HTML滚动条样式 (34)
- HTML5 浏览器支持 (33)
- HTML5 新元素 (33)
- HTML5 WebSocket (30)
- HTML5 代码规范 (32)
- HTML5 标签 (717)
- HTML5 标签 (已废弃) (75)
- HTML5电子书 (32)
- HTML5开发工具 (34)
- HTML5小游戏源码 (34)
- HTML5模板下载 (30)
- HTTP 状态消息 (33)
- HTTP 方法:GET 对比 POST (33)
- 键盘快捷键 (35)
- 标签 (226)
- HTML button formtarget 属性 (30)
- CSS 水平对齐 (Horizontal Align) (30)