百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术分类 > 正文

VUE入门教程(vue快速入门)

ztj100 2025-03-25 19:40 12 浏览 0 评论

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("

hello world

")}

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文件

        Title    <script src="dist/js/bundle.js"></script>

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中配置请求路由


<script>
export default {
  name: 'App',
  components: {
  }
}
</script>

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组件,内容如下:


<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>

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中请求路由


<script>
export default {
  name: 'App',
  components: {
  }
}
</script>

7、测试 npm run dev

注意:如果项目运行失败,可以在package.json里降低sass-loader和node-sass的版本

"sass-loader": "^7.3.1",
"node-sass": "^4.9.0",

嵌套路由

简单说就是在路由里再套一个子路由

1、创建一个作为子路由Profile.vue组件


<script>
    export default {
        name: "List"
    }
</script>

2、Main.vue里请求路由


<script>
  export default {
    name: "Main"
  }
</script>

3、测试

参数传递

参数传递过程:url请求路径—->路由接收参数—->跳转套组件显示参数

1、url请求路径

个人信息

2、路由接收参数

方式一:

 {path:'/user/profile/:id',name:'Profile',component:Profile},

方式二:

{path:'/user/profile/:id',name:'Profile',component:Profile,props:true}

3、组件模板展示参数

方式一:

{ {$route.params.id} }

方式二:


<script>
    export default {
    //接收路由传过来的id
        props:['id'],
        name: "Profile"
    }
</script>

路由钩子与异步请求

路由模式

hash:路径带 # 符号(默认),如 http://localhost/#/login history:路径不带 # 符号,如 http://localhost/login

路由钩子与异步请求

beforeRouteEnter:在进入路由前执行 beforeRouteLeave:在离开路由前执行 类似于过滤器,在进入模板前可以使用路由钩子进行异步请求数据,并在模板展示


<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>

相关推荐

Whoosh,纯python编写轻量级搜索工具

引言在许多应用程序中,搜索功能是至关重要的。Whoosh是一个纯Python编写的轻量级搜索引擎库,可以帮助我们快速构建搜索功能。无论是在网站、博客还是本地应用程序中,Whoosh都能提供高效的全文搜...

如何用Python实现二分搜索算法(python二分法查找代码)

如何用Python实现二分搜索算法二分搜索(BinarySearch)是一种高效的查找算法,适用于在有序数组中快速定位目标值。其核心思想是通过不断缩小搜索范围,每次将问题规模减半,时间复杂度为(O...

路径扫描 -- dirsearch(路径查找器怎么使用)

外表干净是尊重别人,内心干净是尊重自己,干净,在今天这个时代,应该是一种极高的赞美和珍贵。。。----网易云热评一、软件介绍Dirsearch是一种命令行工具,可以强制获取web服务器中的目录和文件...

78行Python代码帮你复现微信撤回消息!

来源:悟空智能科技本文约700字,建议阅读5分钟。本文基于python的微信开源库itchat,教你如何收集私聊撤回的信息。...

从零开始学习 Python!2《进阶知识》 Python进阶之路

欢迎来到Python学习的进阶篇章!如果你说已经掌握了基础语法,那么这篇就是你开启高手之路的大门。我们将一起探讨面向对象编程...

白帽黑客如何通过dirsearch脚本工具扫描和收集网站敏感文件

一、背景介绍...

Python之txt数据预定替换word预定义定位标记生成word报告(四)

续接Python之txt数据预定替换word预定义定位标记生成word报告(一)https://mp.toutiao.com/profile_v4/graphic/preview?pgc_id=748...

假期苦短,我用Python!这有个自动回复拜年信息的小程序

...

Python——字符串和正则表达式中的反斜杠(&#39;\&#39;)问题详解

在本篇文章里小编给大家整理的是关于Python字符串和正则表达式中的反斜杠('\')问题以及相关知识点,有需要的朋友们可以学习下。在Python普通字符串中在Python中,我们用'\'来转义某些普通...

Python re模块:正则表达式综合指南

Python...

Python中re模块详解(rem python)

在《...

python之re模块(python re模块sub)

re模块一.re模块的介绍1.什么是正则表达式"定义:正则表达式是一种对字符和特殊字符操作的一种逻辑公式,从特定的字符中,用正则表达字符来过滤的逻辑。(也是一种文本模式;)2、正则表达式可以帮助我们...

MySQL、PostgreSQL、SQL Server 数据库导入导出实操全解

在数字化时代,数据是关键资产,数据库的导入导出操作则是连接数据与应用场景的桥梁。以下是常见数据库导入导出的实用方法及代码,包含更多细节和特殊情况处理,助你应对各种实际场景。一、MySQL数据库...

Zabbix监控系统系列之六:监控 mysql

zabbix监控mysql1、监控规划在创建监控项之前要尽量考虑清楚要监控什么,怎么监控,监控数据如何存储,监控数据如何展现,如何处理报警等。要进行监控的系统规划需要对Zabbix很了解,这里只是...

mysql系列之一文详解Navicat工具的使用(二)

本章内容是系列内容的第二部分,主要介绍Navicat工具的使用。若查看第一部分请见:...

取消回复欢迎 发表评论: