Vue3 从零搭建项目及插件的使用

node:v14.16.1
vue:@vue/cli 4.5.12

完整代码

:https://download.csdn.net/download/weixin_44590591/76857668

1、项目初始化

1.1、项目前期需求

//使用Vue3,需要升级vue-cli,全局安装最先版本@vue/cli;
npm install -g @vue/cli@next
// 安装成功后输入,显示,安装成功
 vue -V    //@vue/cli 4.5.12
 

1.2、安装成功后搭建Vue3项目

vue create myproject

Vue3 从零搭建项目及插件的使用_第1张图片
1.3、 启动服务

cd myproject  //进入文件
npm run serve // 运行项目

Vue3 从零搭建项目及插件的使用_第2张图片
1.4、项目文件列表

├─.gitignore
├─ .git
├─ node_modules
├─public
|   ├─favicon.ico
|   └index.html
├─src
|  ├─App.vue
|  ├─main.js
|  ├─components
|  |     └HelloWorld.vue
|  ├─assets
|  |   └logo.png
├─babel.config.js
├─package-lock.json
├─package.json
├─README.md

2、配置PostCSS

PostCSS 插件转换 CSS 代码的工具,增加代码的可读性。
利用从 Can I Use 网站获取的数据为 CSS 规则添加特定厂商的前缀。
Autoprefixer 自动获取浏览器的流行度和能够支持的属性,并根据这些数据帮你自动为 CSS 规则添加前缀。
2.1 安装 postcss、postcss-loader、autoprefixer插件

//autoprefixer使用9.8.6版本主要是与postcss的兼容性问题,autoprefixer默认使用最新版本
npm i postcss postcss-loader autoprefixer@9.8.6 -D

注意:该安装是安装当前最新版本的,我这边报了如下错*
Vue3 从零搭建项目及插件的使用_第3张图片
原因: less-loader安装的版本过高
解决方案:卸载重新安装

npm uninstall less-loader
npm install less-loader@6.0.0

2.2 myproject项目下创建postcss.config.js文件;

// postcss.config.js
module.exports = {
    plugins: [
        // 配置 Autoprefixer 插件
        require('autoprefixer')({
            // 游览器最近的两个版本
            'overrideBrowserslist': ['last 2 versions']
        })
    ],
};

2.3 在App.vue 中输入下面代码,重新运行:

<template>
  <div>配置PostCSS</div>
</template>

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

<style scoped>
  div {
    display: flex;
  }
</style>

2.4 通过浏览器查看
配置前:
Vue3 从零搭建项目及插件的使用_第4张图片
配置后:
Vue3 从零搭建项目及插件的使用_第5张图片

3. 配置Css 预处理语言 —— Sass

3.1、安装 Sass;

npm install sass sass-loader -D

//注:该安装是安装当前最新版本的,我这边报了如下错
Vue3 从零搭建项目及插件的使用_第6张图片
原因: sass-loader安装的版本过高
解决方案:卸载重新安装

npm uninstall sass-loader
npm install sass-loader@9.0.0

3.2 App.vue中使用Sass;

<template>
  <div>配置PostCSSdiv>
template>

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

<style lang="scss" scoped>
  $color: red;

  div {
    display: flex;
    color: $color,
  }
style>

Vue3 从零搭建项目及插件的使用_第7张图片

4、 配置UI组件库

这里以 ant-design-vue 和 element 为安装示例,你可以按需求安装组件库
4.1、ant-design-vue@next,ant-design-vue组件已经支持 Vue3

npm i ant-design-vue@next -D
npm install element-plus --save
import {
  createApp
} from 'vue'
import router from './router/index.js'
import App from './App.vue'

import Antd from 'ant-design-vue'

// 导入element-plus和样式文件
import Element from 'element-plus'
import 'element-plus/dist/index.css'
const app = createApp(App)
// 全局组件库
app.use(Element)
app.use(Antd)
app.use(router)
// 挂载视图放在最后一步
app.mount("#app")

Vue3 从零搭建项目及插件的使用_第8张图片
4.2 在页面中引入两个UI组件

<template>
    
    
    <h1>element-plus:h1>
    <el-row class="mb-4">
        <el-button disabled>Defaultel-button>
        <el-button type="primary" disabled>Primaryel-button>
        <el-button type="success" disabled>Successel-button>
        <el-button type="info" disabled>Infoel-button>
        <el-button type="warning" disabled>Warningel-button>
        <el-button type="danger" disabled>Dangerel-button>
    el-row>


    
    <h1>ant-design-vue:h1>
    <div :style="{ background: 'rgb(190, 200, 200)', padding: '26px 16px 16px' }">
        <a-button type="primary" ghost>Primarya-button>
        <a-button ghost>Defaulta-button>
        <a-button type="dashed" ghost>Dasheda-button>
        <a-button danger ghost>Dangera-button>
        <a-button type="link" ghost>Linka-button>
    div>

template>

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

<style lang="scss" scoped>
  $color: red;

  div {
    display: flex;
    color: $color,
  }
style>

Vue3 从零搭建项目及插件的使用_第9张图片

5、路由配置: vue-router

5.1 安装

npm install vue-router@next --save

5.2 安装成功后,在src中创建views 文件夹,并且创建两个文件,文件名字根据需求创建
home.vue

<template>
    <div> home页面div>

template>
<style scoped>
    div {
        font-size: 60px;
    }
style>

about.vue

<template>
    <div>about页面div>
    

template>
<style scoped>
    div {
        font-size: 60px;
    }
style>

Vue3 从零搭建项目及插件的使用_第10张图片
5.3 路由文件配置:在src中新建router 文件夹,并且创建index.js文件如下:

// 导入路由模块
import {
    createRouter,
    createWebHashHistory,
} from 'vue-router'
// 定义路由规则
const Home = ()=> import('./../views/home.vue')
const About = ()=> import('./../views/about.vue')

const routes = [
     {
        path: '/',
        redirect: '/home'
    },{
        path: '/home',
        component: Home
    },
    {
        path: '/about',
        component: About
    },
   
]
export default createRouter({
    // 引用路由规则
    routes,
    // 指定路由模式,
    history: createWebHashHistory(),
});

5.4 main.js 中引用 vue-router;

//main.js
import router from './router/index.js'
...
app.use(router)

Vue3 从零搭建项目及插件的使用_第11张图片
5.5、页面路由跳转
app.vue

<template>
	
  <div>
    <router-link to="/home">homerouter-link>
  div>
  <div>

    <router-link to="/about">aboutrouter-link>
  div>
  <router-view>router-view>
  

template>

<script>
  export default {
    name: 'App',
    methods: {

    }
  }
script>

<style lang="scss" scoped>
  $color: red;


  div {
    display: flex;
    color: $color;
    font-size: 14px;

  }
style>

你可能感兴趣的:(Vue3,vue.js,前端,Vue3)