vue3+vite+ts+elementplus创建项目

ubuntu 安装16以上的需要先执行:

curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -

 安装:apt install nodejs

错误1:./App.vue下面有红色下划线时在vite-env.d.ts中添加下面代码:

declare module '*.vue'{
    import type {DefineComponent} from 'vue'
    const component:DefineComponent<{},{},any>
    export default component
}

设置vue3.2局域网可访问配置

1、找到项目路径下的package.json目录下找到script对象下面添加一下代码:

       "serve": "vite --host 0.0.0.0"

2、启动项目命令不在是dev而是:npm run serve

 配置@路径报错问题:

1、安装path路径:npm install @types/node

2、第二部:在vite.config.ts中添加以下代码配置:

       import { resolve } from 'path';

        export default defineConfig({

                plugins:[vue()],

                resolve:{

                        alias:{

                                '@':resolve(__dirname,'./src')

                        }

                }

        })

例如:

3、第三部:在tsconfig.json中找到"compilerOptions"中添加以下代码:

        "baseUrl":".",

        "paths":{

                "@/*":["src/*"]

        }

        例如:

4、第四部测试是否成功:在App.vue页面设置

import HelloWorld from '@/components/HelloWorld.vue';

<img src="@/assets/1.jpg" class="logo vue" alt="Vue logo" />

测试看看是否成功

# vue3+vite+ts+pinia 学习笔记

## 1、创建项目: npm init vite@latest

    选择: vue、ts

## 2、进入项目目录后:npm install 安装

## 3、运行项目: npm run dev

## 4、安装常用插件:

    1、安装 npm install vue-router@latest 配置:在src目录下新建router目录,创建index.ts文件代码如下:      

```javascript 创建路由文件       

 import { createRouter, createWebHistory } from 'vue-router'

                import Layout from '@/views/layout/index.vue'

                import Login from '@/views/login/Login.vue'

                import Home from '@/views/home/Home.vue'

                // 懒加载

                const Product = ()=>import('@/views/product/Index.vue')

                const List = ()=>import('@/views/product/list/List.vue')

                const Category = ()=>import('@/views/product/category/Category.vue')



                const order = ()=>import('@/views/order/Index.vue')

                const order_List = ()=>import('@/views/order/list/List.vue')

                const order_Collect = ()=>import('@/views/order/collect/Collect.vue')



                const router = createRouter({

                history: createWebHistory(import.meta.env.BASE_URL),

                routes: [

                    {

                    path:'/',

                    name:'',

                    component:Layout,

                    children:[

                        {

                        path:'/',

                        name:'home',

                        component:Home

                        },

                        {

                        path:'/product',

                        name:'product',

                        component:Product,

                        children:[

                            {

                            path:'list',

                            name:'list',

                            component:List

                            },

                            {

                            path:'category',

                            name:'category',

                            component:Category

                            }

                        ]

                        },

                        {

                        path:'/order',

                        name:'order',

                        component:order,

                        children:[

                            {

                            path:'order_list',

                            name:'order_List',

                            component:order_List

                            },

                            {

                            path:'order_collect',

                            name:'order_collect',

                            component:order_Collect

                            }

                        ]

                        },

                       

                    ]

                    },{

                    path:'/login',

                    name:'login',

                    component:Login

                    }

                ]

                })



                export default router

       

```

```javascript 注册路由组件   

注册路由组件:在main.ts文件中注册:

    import router from './router'

    app.use(router)

```

    2、安装 npm install pinia 配置:在src目录下新建stores目录,创建counter.ts文件代码如下:

```javascript 创建仓库文件   

import { ref, computed } from 'vue'

    import { defineStore } from 'pinia'



    export const useCounterStore = defineStore('counter', () => {

    const count = ref(0)

    const doubleCount = computed(() => count.value * 2)

    function increment() {

        count.value++

    }

    return { count, doubleCount, increment }

    })



    export const useUser = defineStore('user',()=>{

    const user = ref({

        name:'刘德华',

        age:64

    })

    return {user}

    })

```

```javascript 注册路由组件   

import { createPinia } from 'pinia'

    const pinia = createPinia()

    app.use(pinia)

```

    2、npm install element-plus --save

```javascript 完整导入 (推荐使用)   

import ElementPlus from 'element-plus'

    import 'element-plus/dist/index.css'

    app.use(ElementPlus)

```

```javascript 按需导入 (需要安装插件:npm install -D unplugin-vue-components unplugin-auto-import) 在vite.config.ts中修改一下代码   

 import { defineConfig } from 'vite'

    import AutoImport from 'unplugin-auto-import/vite'

    import Components from 'unplugin-vue-components/vite'

    import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'



    export default defineConfig({

        plugins: [

            vue(),

            // ...

            AutoImport({

            resolvers: [ElementPlusResolver()],

            }),

            Components({

            resolvers: [ElementPlusResolver()],

            }),

        ],

    })

   

```

```javascript element-plus图标显示   

 <script lang="ts" setup>

        import {Check,Delete,Edit,Message,Search,Star,} from '@element-plus/icons-vue'

    </script>

```  

    3、npm i axios -S 直接安装即可使用

    5、npm i --save-dev @types/qs 直接安装即可使用

    6、npm i echarts -S

```javascript echarts按需导入使用教程

    1、在src目录下创建一个文件夹,命名为插件:plugins目录,创建一个echarts.ts文件复制一下内容至文件:   

// 图表

    import * as echarts from 'echarts/core';

    import {BarChart,LineChart,PieChart} from 'echarts/charts';

    import {TitleComponent,TooltipComponent,GridComponent,DatasetComponent,TransformComponent} from 'echarts/components';

    import { LabelLayout, UniversalTransition } from 'echarts/features';

    import { CanvasRenderer } from 'echarts/renderers';

    import type {BarSeriesOption, LineSeriesOption} from 'echarts/charts';

    import type {TitleComponentOption,TooltipComponentOption,GridComponentOption,DatasetComponentOption} from 'echarts/components';

    import type { ComposeOption, } from 'echarts/core';



    // 通过 ComposeOption 来组合出一个只有必须组件和图表的 Option 类型

    type ECOption = ComposeOption<

    | BarSeriesOption

    | LineSeriesOption

    | TitleComponentOption

    | TooltipComponentOption

    | GridComponentOption

    | DatasetComponentOption

    >;



    // 注册必须的组件

    echarts.use([

    TitleComponent,

    TooltipComponent,

    GridComponent,

    DatasetComponent,

    TransformComponent,

    BarChart,

    LineChart,

    PieChart,

    LabelLayout,

    UniversalTransition,

    CanvasRenderer

    ]);



    export default echarts

    2、调用页面代码实例:

    import echarts from '@/plugins/echarts';

    // let myEchart1 = echarts.init(document.getElementById("draw")) // 切换页面时不会显示,解决办法使用下面的

    // 解决一个charts不渲染的问题,认为dom没有修改

    let dsiab_com = document.getElementById("draw")

    dsiab_com.removeAttribute("_echarts_instance_")

    var myEchart1 = echarts.init(dsiab_com)

    // 绘制图表

    myEchart1.setOption({

        tooltip: {},

        xAxis: {

        data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子']

        },

        yAxis: {},

        series: [

        {

            name: '销量',

            type: 'line', // line折线图,bar柱状图,pie

            data: [15, 210, 362, 103, 10, 20]

        },

        {

            name: '金额',

            type: 'bar', // line折线图,bar柱状图,pie

            data: [5, 20, 36, 10, 10, 20]

        }

        ]

    });

```

    7、npm i sass sass-loader -D 直接安装即可使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用Vue3 + TypeScript + Vite + Element Plus + Router + Axios搭建前端项目框架的步骤: 1. 首先,确保你已经安装了Node.js和npm。你可以在命令行中运行以下命令来检查它们的版本: ```shell node -v npm -v ``` 2. 创建一个新的项目文件夹,并在该文件夹中打开命令行。 3. 在命令行中运行以下命令来初始化一个新的Vite项目: ```shell npm init vite ``` 在初始化过程中,你需要选择Vue作为模板,选择TypeScript作为语言,并填写项目名称。 4. 进入项目文件夹,并安装所需的依赖: ```shell cd your-project-name npm install ``` 5. 安装Vue Router、Vuex和Axios: ```shell npm install vue-router@next vuex@next axios ``` 6. 在项目文件夹中创建一个新的文件夹,用于存放页面组件和路由配置文件。 7. 在src文件夹中创建一个新的文件夹,用于存放页面组件。 8. 在src文件夹中创建一个新的文件夹,用于存放路由配置文件。 9. 在src/router文件夹中创建一个新的文件,命名为index.ts,并在其中编写路由配置: ```typescript import { createRouter, createWebHistory } from 'vue-router'; import Home from '../views/Home.vue'; const routes = [ { path: '/', name: 'Home', component: Home, }, // 添加其他页面的路由配置 ]; const router = createRouter({ history: createWebHistory(), routes, }); export default router; ``` 10. 在src/main.ts文件中导入并使用Vue Router: ```typescript import { createApp } from 'vue'; import App from './App.vue'; import router from './router'; createApp(App).use(router).mount('#app'); ``` 11. 在src/views文件夹中创建一个新的文件,命名为Home.vue,并在其中编写一个简单的页面组件: ```vue <template> <div> <h1>Welcome to Home Page</h1> </div> </template> <script> export default { name: 'Home', }; </script> ``` 12.src/App.vue文件中添加一个路由出口,用于显示组件: ```vue <template> <div id="app"> <router-view></router-view> </div> </template> <script> export default { name: 'App', }; </script> ``` 13. 在src/main.ts文件中导入并使用Element Plus: ```typescript import { createApp } from 'vue'; import App from './App.vue'; import router from './router'; import ElementPlus from 'element-plus'; import 'element-plus/lib/theme-chalk/index.css'; createApp(App).use(router).use(ElementPlus).mount('#app'); ``` 14. 运行以下命令来启动开发服务器: ```shell npm run dev ``` 15. 打开浏览器,并访问http://localhost:3000,你将看到一个简单的页面,其中包含"Welcome to Home Page"的文本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cyz141001

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值