首页 > 其他分享 >vue3项目的创建与初始化(vite)

vue3项目的创建与初始化(vite)

时间:2022-09-06 12:33:40浏览次数:56  
标签:初始化 vue import app js vite vue3 router store

node: 16版本 npm:8版本

一、创建

1. 以下代码:注意my-vue-app为即将创建项目的名字,可以自行更改

# npm 6.x
npm create vite@latest my-vue-app --template vue

# npm 7+, extra double-dash is needed:
npm create vite@latest my-vue-app -- --template vue

# yarn
yarn create vite my-vue-app --template vue

# pnpm
pnpm create vite my-vue-app -- --template vue

2.安装依赖

npm install

3. 启动

npm run dev

4.安装volar

image

二、初始化

1、完善目录

在src中增加:

router/index.js:路由配置,router文件夹下建立一个index.js文件

store/index.js:状态管理。vuex配置。store文件夹下建立一个index.js文件

api/index.js:调用接口。

untils/index.js:工具。存放axios以及拦截器

views:页面。views文件夹

2、修改App.vue、main.js

将App.vue内容改为

<template>
  <router-view/>
</template>

<script setup>
</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>

将 main.js内容修改为:

import { createApp } from 'vue'
import App from './App.vue'

let app = createApp()
app.mount('#app')

3、 路由配置与使用

路由的配置

安装

npm install vue-router@4

在router/index.js文件中修改为如下代码:

import{
  createRouter,
  createWebHashHistory,
  createWebHistory,
}from "vue-router";


//定义一些路由
//每个路由需要定义一个组件,
const routes = [
  // {path:"/",component:Home,name:"home"} //不带参
  // {path:"/cart:id",component:()=>import("../views/cart.vue"),name:"cart"}
]

//穿件路由实例并传递“routes”配置
//可以在这里输入更多的配置
const router = createRouter({
  //内部提供了history模式的实现,为了简单起见,我们这里使用hash模式
  history:createWebHashHistory(),
  routes, //`routes:routes`的缩写
})

export default router;

在main.js中全局配置路由:

import router from "./router/index"
app.use(router)

image

路由的使用

(1)组件上属性跳转

<router-link to="/"/>

(2)事件中使用路由跳转

import {useRoute,useRouter} from "vue-router"
const route = useRoute()  //route是获取路由信息的
const router = useRouter() //router是进行路由跳转的

//不带参跳转
funtion toHome(){
	router.push('/')
}

//带参跳转
funtion toCart(){
	router.push({
        name:'cart',
        query:{
            id:34
        }
	})
}

4、状态管理配置与使用

状态管理配置

(1)安装vuex4

npm install vuex@next --save
或
yarn add vuex@next --save

在 store/inde.js 中内容修改为,数据为例子,可以删除

import { createStore } from 'vuex'

// 创建一个新的 store 实例
const store = createStore({
  state () {
    return {
      count: 0
    }
  },
  //方法控制数据的改变
  mutations: {
    increment (state,data) {
      state.count+=data
    }
  },
  //计算属性
  getters:{
    //比如计算商品的总价
    totalPrice(state){
      return state.count*9.99
    }
  },

  action:{
    asyncAdd(store,data){
      setTimeout(()=>{
        store.commit("increment",data);
      },1000)
    }
  }
})

export default store;

在main.js中配置

import store from "../store/index"
app.use(store)
store的使用

(1)页面中使用state和getters中的数据

<div>{{store.state.count}}</div>
<div>{{store.getters.totalPrice}}</div>

(2)页面script中使用方法

import {useStore} from "vuex"
const store = useStore();

//调用mutations中的方法(同步)
function add(){
	store.commit("increment",2);
}

//调用 actions 中的方法(异步)
function add2(){
    store.dispatch("asyncAdd",10)
}

5、ant desgin vue组件的导入与使用

安装
npm i --save ant-design-vue
注册(全局)

在 main.js中导入

import Antd from 'ant-design-vue';
import 'ant-design-vue/dist/antd.css';
app.use(Antd)

6、api配置与使用

安装axios
npm i axios 

untils/index.js 或 直接在api/index.js中写入如下代码

import axios from "axios"

const request = axios.create({
	//配置接口请求基准路径
	baseURL = ""
    //baseURL:"http://api.cpengx.cn/metashop/api",
});

//响应拦截器
request.interceptors.response.use(
	//成功的时候执行
	(rersponse)=>{
		if(response.status == 200){
			return response.data;
		}else{
			return response;
		}
	},
	//请求失败的时候
	function(error){
		return Promise.reject(error);
	}
);

标签:初始化,vue,import,app,js,vite,vue3,router,store
From: https://www.cnblogs.com/huangchun/p/16661364.html

相关文章

  • vue3——toRaw 与 markRaw
    toRaw:作用:将一个由reactive生成的响应式对象转为普通对象。使用场景:用于读取响应式对象对应的普通对象,对这个普通对象的所有操作,不会引起页面更新。markRaw:作......
  • NET5配合vue3图片上传
    NET5配合vue3图片上传后端函数///<summary>///上传文件///</summary>///<paramname="files">文件流</param>///<ret......
  • vue2进阶vue3环境搭建
    一、cli升级sudocnpminstall-g@vue/cli二、查看版本vue--version 目前最新为 @vue/cli5.0.8 vue/cli要4.0以上的版本才有创建vue3的模版 三、升级node......
  • vue3项目-小兔鲜儿笔记-商品详情页02
    1.SKU和SPU概念SPU代表一个商品,这个商品可以拥有很多属性SKU代表这个商品可选规格的任意组合,是库存单位唯一标识 2.路径字典大致步骤根据后台返回的sku数据得......
  • vue3+ts 为原型上添加属性声明
    背景比如在vue2的情况下我们有的时候需要为Vue的原型对象上添加属性或方法,vue3的情况下需要为app挂载全局属性配置,但是结合了ts后,在vue文件中会报警告,但是程序可以正常被......
  • vue3之composition-api的使用(包含watch watchEffect)
    是什么composition-api官方介绍vue2:options-api,组件按照选项组织,就是将组件各个部分严格写在methods、computed、watch、data等等里面(特定的区域写特定的代码);vue3:co......
  • vue3 组件-动画进度条
    https://kuangyx.cn/docs/文章/vue3组件/进度条.html......
  • vue3 组件-通知菜单
    https://kuangyx.cn/docs/文章/vue3组件/通知菜单.html......
  • vue3 组件-表格分页
    typescript类型提示(属性、方法、el-table与el-pagination自带ts类型)json配置el-table控制栏自定义单元格编辑编辑行自动请求接口接口请求参数与响应数据路径......
  • vue3 组件-开始结束日期选择器
    https://kuangyx.cn/docs/文章/vue3组件/时间选择.html......