简单搭建vue项目

1.先安装node.js和vite,具体参考:

2.管理员身份运行cmd,跳转到node安装目录:

输入: npm create vite@latest

输入项目名称,选择vue和JavaScript

2.VisualStudioCode打开(可能需要管理员权限)创建的文件夹,点击左侧选中

Crtrl加~打开终端

输入:npm install vue-router  配置路由

输入:  npm install axios  安装axios

输入:npm install element-plus --save  安装element-plus

3.配置文件

src目录下创建router、util、api、views文件夹。

        api文件夹下创建index.js文件,放入代码:

import http from '../util/http.js';

//get:(url)=>{http({url:url,method:'get'})}
//{}代表方法体,没有return就没有返回值了
//get:(url)=>{http(return {url:url,method:'get'})}
export const API={
    //get方法,传入url参数
    get:(url)=>http({url:url,method:'get'})
};
//抛出一个实例
export default API;

        router文件夹下创建index.js文件,放入代码:

import {createRouter,createWebHashHistory} from 'vue-router';

const router=createRouter(
    {
        history:createWebHashHistory(),
        routes:[
            {path:'/easya',component:()=>import("../views/easya.vue")}

        ]
    }
);
export default router;

        util文件夹下创建文件http.js,放入代码:

import axios from 'axios';

export default function (options) {
    //配置每次发送请求都从sessionStorage中获取名字叫token的数据,
    //添加到请求头部的Authorization属性中

    //Object.assign用于合并对象的数据
    options.headers = Object.assign(
        { Authorization: sessionStorage.getItem('token') },
        options.headers || {}
    );
    //axios()   返回一个promise对象,用于异步请求
    //options是一个对象,其中包含了许多用于配置请求的参数,
    //例如请求的url、请求方法(GET、POST等)、请求头等
    return axios(options)
        .then(({ status, data, statusText }) => {
            //该函数在请求成功并返回数据时被调用
            //status:HTTP状态码,例如200表示请求成功。
            //data:服务器返回的数据。
            // statusText:HTTP状态文本,例如"OK"表示请求成功。
            if (status == 200) {
                return data;
            } else {
                throw new Error(statusText);
            }
        }).catch(e=>{
            return Promise.reject(e);
            //return Promise.reject(e.message);
        });
}

        views文件夹下创建文件easya.vue,放入代码做测试:

<script setup>
import {ref,onMounted} from 'vue';
import easyapi from '../api';

</script>

<template>
   
  <h1>hello vue</h1>

</template>
<style>
</style>

src下的main.js代码覆盖为:

import { createApp } from 'vue'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import App from './App.vue'
import router from './router'

createApp(App).use(router).use(ElementPlus).mount('#app')

src下的App.vue代码覆盖为:

<script setup>

</script>

<template>
  <!--设置页面占位符-->
  <router-view></router-view>
</template>

运行测试:输入 npm run dev

访问本地路径:http://localhost:5173/easya 访问

终端Ctrl+c退出

  • 6
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供有关如何使用Webpack 4来构建Vue项目的步骤: 1. 创建项目文件夹并初始化npm(如果您尚未完成): ``` mkdir your-project-name cd your-project-name npm init -y ``` 2.\u4e3a\u9879\u76ee\u6dfb\u52a0Webpack 4\uff0c\u60a8\u53ef\u4ee5\u9047\u5230\u4ee5\u4e0b\u6b65\u9aa41\u3002\u5728\u9879\u76ee\u7684\u6839\u76ee\u5f55\u4e2d\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u5939\u4ee5\u7ed3\u6784\u4e00\u4e2a\u7f16\u7801\u6587\u4ef6\u5e76\u5c06\u5b83\u5f00\u59cb\u8bbe\u7f6e\u4e3awebpack\u3002 ``` mkdir src touch src/index.js ``` 3. \u5728\u9879\u76ee\u5e93\u4e2d\u5b89\u88c5Webpack 4\uff0c\u4f7f\u7528npm\u5b89\u88c5webpack\u548cwebpack-cli\u3002 ``` npm install webpack webpack-cli --save-dev ``` 4. \u5728\u9879\u76ee\u5e93\u6839\u76ee\u5f55\u4e2d\u6dfb\u52a0\u4e00\u4e2awebpack.config.js\u6587\u4ef6\uff0c\u5e76\u914d\u7f6eWebpack\u7684\u914d\u7f6e\u9879\u3002 ``` touch webpack.config.js ``` 5. \u7f16\u8f91webpack.config.js\u6587\u4ef6\uff0c\u914d\u7f6e\u9879\u76ee\u7684\u8f93\u51fa\u6587\u4ef6\u540d\u79f0\u548c\u8f93\u51fa\u76ee\u5f55\u3002 ``` const path = require('path'); module.exports = { entry: './src/index.js', output: { filename: 'bundle.js', path: path.resolve(__dirname, 'dist'), }, }; ``` 6. \u7f16\u8f91src/index.js\u6587\u4ef6\uff0c\u4e3aVue\u6a21\u5757\u6dfb\u52a0\u4ee5\u53ca\u8fd0\u884c\u4e3aVue\u6a21\u5757\u7684\u5e93\u3002\u5728\u6b64\u4f7f\u7528npm\u5b89\u88c5Vue\u3002 ``` npm install vue --save ``` 7. \u7f16\u8f91src/index.js\u6587\u4ef6\uff0c\u5e76\u5b9e\u73b0\u5728\u9879\u76ee\u4e2d\u4f7f\u7528Vue\u6a21\u5757\u3002 ``` import Vue from 'vue'; new Vue({ el: '#app', template: '<div>Hello, Vue!</div>', }); ``` 8. \u521b\u5efa\u9879\u76ee\u7684index.html\u6587\u4ef6\uff0c\u4e3a好的,让我来简单介绍一下如何使用Webpack 4来构建Vue项目: 1. 安装Node.js和npm 确保你的电脑上已经安装了Node.js和npm。你可以通过访问Node.js官方网站 (https://nodejs.org/) 下载并安装。 2. 创建项目文件夹 在命令行中创建一个新的项目文件夹,例如: ``` mkdir my-vue-project ``` 3. 初始化npm 进入该项目文件夹,并在命令行中输入以下命令来初始化npm: ``` cd my-vue-project npm init -y ``` 4. 安装Vue.js 使用npm安装Vue.jsVue Loader: ``` npm install vue vue-loader vue-template-compiler --save-dev ``` 5. 安装Webpack 4及其相关插件 使用npm安装Webpack 4及其相关插件: ``` npm install webpack webpack-cli webpack-dev-server html-webpack-plugin --save-dev ``` 6. 创建Webpack配置文件 在项目根目录下创建一个webpack.config.js文件,并在其中添加以下内容: ``` const HtmlWebpackPlugin = require('html-webpack-plugin'); const path = require('path'); module.exports = { entry: './src/main.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js', }, module: { rules: [ { test: /\.vue$/, loader: 'vue-loader', }, { test: /\.js$/, exclude: /(node_modules)/, use: { loader: 'babel-loader', options: { presets: ['@babel/preset-env'], }, }, }, ], }, plugins: [ new HtmlWebpackPlugin({ template: './src/index.html', }), ], devServer: { contentBase: path.join(__dirname, 'dist'), compress: true, port: 9000, }, }; ``` 7. 创建项目文件夹结构 在项目根目录下创建一个src文件夹,并在其中创建一个main.js文件和一个index.html文件。在src文件夹中再创建一个components文件夹,并在其中创建一个HelloWorld.vue组件文件。具体的文件夹结构如下: ``` my-vue-project/ src/ components/ HelloWorld.vue main.js index.html webpack.config.js package.json ``` 8. 编写Vue组件 打开HelloWorld.vue文件,并添加以下代码: ```html <template> <div> <h1>{{ msg }}</h1> </div> </template> <script> export default { name: 'HelloWorld', data() { return { msg: 'Welcome to my Vue.js app!', }; }, }; </script> ``` 9. 编写入口文件 打开main.js文件,并添加以下代码: ```javascript import Vue from 'vue'; import HelloWorld from './components/HelloWorld.vue'; new Vue({ el: '#app', components: { HelloWorld, }, template: '<HelloWorld/>', }); ``` 10. 编写HTML文件 打开index.html文件,并添加以下代码: ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>My Vue.js好的,以下是使用webpack4搭建Vue项目的基本步骤: 1. 创建项目文件夹,并初始化为npm项目 ``` mkdir my-project cd my-project npm init -y ``` 2. 安装必要的依赖包 ``` npm install webpack webpack-cli vue vue-loader vue-template-compiler css-loader style-loader --save-dev ``` 3. 在项目根目录下创建webpack配置文件webpack.config.js ``` const path = require('path'); const VueLoaderPlugin = require('vue-loader/lib/plugin'); module.exports = { entry: './src/main.js', output: { path: path.resolve(__dirname, 'dist'), filename: 'bundle.js' }, module: { rules: [ { test: /\.vue$/, use: 'vue-loader' }, { test: /\.css$/, use: ['style-loader', 'css-loader'] } ] }, plugins: [ new VueLoaderPlugin() ] }; ``` 4. 在src文件夹下创建一个主入口文件main.js和App.vue组件 ``` // main.js import Vue from 'vue'; import App from './App.vue'; new Vue({ el: '#app', render: h => h(App) }); // App.vue <template> <div> <h1>Hello Vue!</h1> </div> </template> <script> export default { name: 'App' }; </script> <style> h1 { color: red; } </style> ``` 5. 在index.html中创建一个div容器,并引入打包后的bundle.js文件 ``` <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Vue Project</title> </head> <body> <div id="app"></div> <script src="./dist/bundle.js"></script> </body> </html> ``` 6. 在package.json中添加启动脚本,方便快速启动项目 ``` "scripts": { "start": "webpack --mode development --watch" }, ``` 7. 在命令行中输入npm start,即可启动项目。在浏览器中打开index.html,即可看到Vue应用程序运行的效果。 希望这个回答能对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值