热门课程

免费试听

上课方式

开班时间

当前位置: 首页 -   文章 -   新闻动态 -   正文

深入理解VUE

知了堂姐
2024-07-09 11:12:24
0

vue-cli是官方提供的一个脚手架,用于快速生成一vue项目,有点类似java中使用maven构建项目

需要环境

Node.js : 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

打开浏览器输入 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    

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>

4、测试

 

参数传递

参数传递过程 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:路径带 # 符号(默认),如 localhost/# history:路径不带 # 符号,如 localhost/login

路由钩子与异步请求

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



大家都在看

什么是交换机?交换机的工作原理是什么?

2024-07-09 浏览次数:0

测试学员参与商业项目,拿下人生第一笔“工资”!

2024-07-09 浏览次数:0

html和xhtml有什么区别?html和XHT...

2024-07-09 浏览次数:0

0基础参加Java培训一般多久能找到工作?

2024-07-09 浏览次数:0

信息安全行业含金量较高的2个认证是那些?想转行网...

2024-07-09 浏览次数:0

昨晚,你与H5擦出火花了吗

2024-07-09 浏览次数:0
最新资讯
Element UI组件介绍 1、安装Element UI,通过vue脚手架创建项 vue init webpack elemen...
深入理解VUE vue-cli是官方提供的一个脚手架,用于快速生成一vue项目,有点类似java中使用maven构建...
react和vue的区别,re... react和vue的区别及优缺点为什么不直接使用渲染层API?跨终端开发的痛苦之一是学习,使用和维护...
web前端框架哪个好,layu... 很难预测React和Redux 的未来。但是,将库集中在一起,确实会显着提高适应性,大多数React...
react样式,react v... react vue选哪一个请注意,此处的css样式名称使用驼峰式命名方法:例如→,并且需要将CSS属...
知了堂&前端:手写Vue2系列... 本篇的目标是实现 computed 计算属性,完成模版中计算属性的展示。
不用 vue-cli 如何搭建... 需要使用构建工具 webpack(当然还有其他工具 gulp,browserify),通过 entr...
Vue 如何实现数据的双向绑定... Vue 通过数据劫持(Object.defineProperty())+ 订阅发布模式 监听器 Ob...
<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>

4、测试

 

参数传递

参数传递过程 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:路径带 # 符号(默认),如 localhost/# history:路径不带 # 符号,如 localhost/login

路由钩子与异步请求

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



VUE

上一篇:JavaWeb项目部署到Linux服务器(新手教学)

下一篇:深入理解java虚拟机——内存区域与内存溢出异常

相关内容

Element UI组件介...
1、安装Element UI,通过vue脚手架创建项 vue in...
2024-07-09 11:12:24
深入理解VUE
vue-cli是官方提供的一个脚手架,用于快速生成一vue项目,有...
2024-07-09 11:12:24
react和vue的区别,...
react和vue的区别及优缺点为什么不直接使用渲染层API?跨终...
2024-07-09 11:12:24
web前端框架哪个好,la...
很难预测React和Redux 的未来。但是,将库集中在一起,确实...
2024-07-09 11:12:24
react样式,react...
react vue选哪一个请注意,此处的css样式名称使用驼峰式命...
2024-07-09 11:12:24
知了堂&前端:手写Vue2...
本篇的目标是实现 computed 计算属性,完成模版中计算属性的...
2024-07-08 17:22:16

热门资讯

就业课程介绍(Java+前端+... Java+大数据,前端全栈,信息安全
关于我们 请输入文章描述
0基础转行信安,他如何做到月薪... 转行并非简单的换份工作,而是我们在职场进行自我认同、重塑身份的一个过程。今天知了小姐姐为大家介绍一位...
【前端每日一题】什么是BFC?... 秋招马上就要开始了,小伙伴们最近在准备面试的东西没呢,知了姐今天将蛋糕哥整理的前端面试题共享出来,同...
cisp考试费用多少?cisp... 随着网络技术的快速发展,网络安全问题变得越来越重要。那么,CISP考试费用多少?CISP报名条件是什...
pythone 文件和数据格式... 关于 Python 对文件的处理,以下选项中描述错误的是
img标签的onerror事件... 1.img 标签除了 onerror 属性外,还有其他获取管理员路径的办法吗? src 指定一个远程...
知了堂官网V3第一版内测邀请 经过几个月的加班加点,我们终于迎来了知了堂官网3.1.0版本 现正招募内测中
网络安全运维岗面试题及答案详解... 在当今数字化时代,网络安全运维工程师的角色变得愈发重要。为了保障网络安全,各个企业都需要拥有一支经验...
川农第一次线下拓展精彩瞬间 5月15日,知了堂的哥哥姐姐们携手企业拓展教练浩浩荡荡奔赴川农。
-->