vue2创建项目的两种方式,配置路由vue-router,引入element-ui

提示:vue2依赖node版本8.0以上

文章目录


前言

使用@vue/cli脚手架vue create创建

使用vue-cli脚手架vue init webpack创建

一、创建项目基于vue-cli

1、查看nodejs版本

c 复制代码
node -v

2、全局安装vue脚手架和webpack脚手架

c 复制代码
npm install -g vue-cli
npm install -g webpack
npm install -g webpack-cli


3、新建vue2项目

c 复制代码
vue init webpack test-vue2-webpack

创建选项除了,Install vue-router??选择是,其他选择的否

4、安装依赖并启动文件

c 复制代码
 cd test-vue2-webpack
  npm install
  npm run dev

5、预览

6、目录结构

二、创建项目基于@vue/cli

1、如果安装了vue-cli,需要先卸载vue-cli

c 复制代码
 npm uninstall -g vue-cli

2、全局安装vue脚手架和webpack脚手架

c 复制代码
npm install -g @vue/cli
npm install -g webpack
npm install -g webpack-cli


3、新建vue2项目

c 复制代码
 vue create test-vue2-create

创建选项选择vue2项目


4、启动文件

这样创建的项目,不需要npm install,直接可启动

c 复制代码
 cd test-vue2-create
 npm run serve

5、预览

6、目录结构

三、对吧两种创建方式

1、使用@vue/cli脚手架vue create创建项目,结构更简单,创建完直接安装完依赖,不需要手动npm install

2、使用@vue/cli脚手架vue create创建项目,安装的依赖更少

3、启动方式,启动方式可以自己配置,看个人习惯

c 复制代码
//vue-cli  
npm run dev
//@vue/cli  
npm run serve

4、package.json配置npm run dev启动命令

四、安装Element ui并引入

安装和引用element-ui方式都相同,这里拿vue create 的test-vue2-create项目做演示

1、安装

c 复制代码
npm i element-ui -S

2、引入

main.js

c 复制代码
import Vue from 'vue'
import App from './App.vue'
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

Vue.config.productionTip = false

Vue.use(ElementUI);

new Vue({
  render: h => h(App),
}).$mount('#app')

3、按需引入

c 复制代码
npm install babel-plugin-component -D

main.js

c 复制代码
import Vue from 'vue'
import App from './App.vue'
import 'element-ui/lib/theme-chalk/index.css';
//全部引用
// import ElementUI from 'element-ui';
// Vue.use(ElementUI);

//按需引用
import { Button,Input } from 'element-ui';
Vue.use(Button).use(Input);

Vue.config.productionTip = false



new Vue({
  render: h => h(App),
}).$mount('#app')

4、配置babel.config.js

babel.config.js

c 复制代码
module.exports = {
  presets: [
    '@vue/cli-plugin-babel/preset'
  ],
  plugins: [
    [
      "component",
      {
        "libraryName": "element-ui",
        "styleLibraryName": "theme-chalk"
      }
    ]
  ]
}

5、修改app.vue

c 复制代码
<template>
  <div id="app">
    <el-button>按钮</el-button>
    <el-input placeholder="输入框"></el-input>
  </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>

6、启动项目预览

c 复制代码
npm run dev

五、配置路由跳转

1、安装vue-router

c 复制代码
npm install vue-router -S 

2、关闭创建vue component检查

vue.config.js

c 复制代码
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
  transpileDependencies: true,
  lintOnSave: false
})

3、新建src/router/index.js

c 复制代码
import Vue from 'vue';
import Router from 'vue-router';
import Home from '@/components/Home.vue';
import My from '@/components/My.vue';
Vue.use(Router);
const routes = [
    { path: '/', component: Home },
    { path: '/my', component: My }
  ]
  
export default new Router({ routes })

4、新建src/components/Home.vue和src/components/My.vue

Home.vue

c 复制代码
<template>
  <div class="home">
    <h1>Home页面</h1>
    <el-button @click="$router.push('/my')">去My</el-button>
  </div>
</template>

<script>
export default {
  name: 'Home',
}
</script>
<style scoped>
</style>

My.vue

c 复制代码
<template>
  <div class="my">
    <h1>My页面</h1>
    <el-button @click="$router.push('/')">去Home</el-button>
  </div>
</template>

<script>
export default {
  name: 'My',
}
</script>
<style scoped>
</style>

5、修改App.vue

c 复制代码
<template>
  <div id="app">
    <router-view></router-view>
  </div>
</template>
<script>
export default {
  name: 'App',
  components: {}
}
</script>
<style>
</style>

6、修改main.js

c 复制代码
import Vue from 'vue'
import App from './App.vue'
import 'element-ui/lib/theme-chalk/index.css';
import router from './router'
//全部引用
// import ElementUI from 'element-ui';
// Vue.use(ElementUI);

//按需引用
import { Button,Input } from 'element-ui';
Vue.use(Button).use(Input);

Vue.config.productionTip = false
new Vue({
  render: h => h(App),
  router
}).$mount('#app')

四、效果


五、参考文档

1、vue2官网

2、Element UI官网

总结

踩坑路漫漫长@~@

相关推荐
学嵌入式的小杨同学7 小时前
从零打造 Linux 终端 MP3 播放器!用 C 语言实现音乐自由
linux·c语言·开发语言·前端·vscode·ci/cd·vim
weixin_425543737 小时前
TRAE CN3.3.25 构建的Electron简易DEMO应用
前端·typescript·electron·vite·nestjs
Mr Xu_8 小时前
【Vue3 + ECharts 实战】正确使用 showLoading、resize 与 dispose 避免内存泄漏
前端·信息可视化·vue·echarts
0思必得08 小时前
[Web自动化] Selenium设置相关执行文件路径
前端·爬虫·python·selenium·自动化
雯0609~8 小时前
hiprint:实现项目部署与打印1-官网提供普通html版本
前端·html
不绝1919 小时前
UGUI——进阶篇
前端
~牧马~9 小时前
【记录63】electron打包vue项目之踩坑
vue.js·electron·electron与node兼容
踏过山河,踏过海9 小时前
【用ui文件做个简单工具的开发,为什么修改完ui后,程序重新编译运行后,GUI界面还是不变呢?】
qt·ui
Exquisite.9 小时前
企业高性能web服务器(4)
运维·服务器·前端·网络·mysql
2501_9445255410 小时前
Flutter for OpenHarmony 个人理财管理App实战 - 账户详情页面
android·java·开发语言·前端·javascript·flutter