Vs code+Vue+Element前端开发环境搭建

Vs code+Vue+Element前端开发环境搭建

Vs code安装

  1. 下载可以访问Visual Studio Code的官方网站,但是由于是国外的镜像源下载很缓慢。
    image-20210604100229622
  2. 下载缓慢的解决方法
  • 复制下载地址,将镜像源换为国内的( vscode.cdn.azure.cn)
    image-20210604100640513
  1. 安装过程很简单不过多赘述,自行安装即可。
  2. VS code安装完成后,安装Vetur
    image-20210604103052597

Vue-Cli脚手架配置

  1. 安装node.js,点击此链接进入官网下载安装即可。
  2. 安装Vue CLI,这里以windows系统为例。
  • 首先打开cmd面板,输入:
    npm install -g @vue/cli
    
  • 等待结束后,输入:
    vue --version
    

正确出现这个时,即为安装成功
image-20210604101658396
3. Vue项目创建(这里以进入E盘的MyUI文件夹创建项目为例)

  • 调出cmd面板,依次输入:
    E:
    cd MyUI
    vue create name //name这里指的是项目名,项目名首字母不能大写,我这里大写了
    

image-20210604102152437

  • enter后开始创建,如下图所示即为创建完成
    image-20210604102539649
  1. 使用Vs code打开我们刚刚创建的项目,如下图所示:
    image-20210604103213399
  • 删除helloworld.vue文件,以及app.vue中与helloworld中的相关代码,app.vue中剩余代码如下所示:
    <template>
      <div id="app">
    
      </div>
    </template>
    
    <script>
    export default {
      name: 'App',
    
    }
    </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>
    

安装element-ui

  1. 安装,打开Vs code的控制终端,输入:

    npm i element-ui -S 
    

    得到此图即为安装成功image-20210604104209346

  2. 在 webpack 入口页面 main.js 添加如下配置:

    import ElementUI from 'element-ui';
    import 'element-ui/lib/theme-chalk/index.css';
    Vue.use(ElementUI);
    

    修改后的main.js如下所示:

    import Vue from 'vue'
    import App from './App.vue'
    import ElementUI from 'element-ui';
    import 'element-ui/lib/theme-chalk/index.css';
    Vue.use(ElementUI);
    Vue.config.productionTip = false
    new Vue({
      render: h => h(App),
    }).$mount('#app')
    
    

安装router

  1. 安装,打开VS code的控制终端,输入:
    npm install vue-router
    
    得到此图即为安装成功image-20210604105238793
  2. 配置router
  • 在项目的src文件夹下新建文件夹router和views,并在router文件夹下新建文件index.js,在views文件夹下新建test文件夹,并新建文件test.vue.
  • 在router的index.js中添加路由
    import Vue from "vue";
    import VueRouter from "vue-router";
    Vue.use(VueRouter);
    const routes = [
      {
        path: "/",
        component: () => import('@/views/test/test'),//这里指向的是views文件夹下test文件夹中的文件test
        hidden: true
      },
    ];
    const router = new VueRouter({
      mode: "history",
      base: process.env.BASE_URL,
      routes
    });
    export default router;
    
  • 将main.js配置修改如下:
    import Vue from 'vue'
    import App from './App.vue'
    import ElementUI from 'element-ui';
    import 'element-ui/lib/theme-chalk/index.css';
    import VueRouter from 'vue-router'
    import router from "./router";
    
    Vue.config.productionTip = false;
    Vue.use(ElementUI);
    Vue.use(VueRouter);
    new Vue({
      router,
      render: h => h(App),
    }).$mount('#app')
    
  • 在app.vue中添加路由容器:
     <router-view></router-view>
    
    app.vue中的代码如下:
    <template>
      <div id="app">
        <router-view></router-view>
      </div>
    </template>
    <script>
    export default {
      name: "App",
    };
    </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>
    
    

项目运行

在Vs code的控制台输入:npm run serve
控制台显示如下,即为成功运行,按住ctrl并单击即可访问浏览器,显示的是一个空白页面。
image-20210604111149237

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用 Django Rest Framework (DRF)、Vue 3 和 Element Plus 搭建的前后端分离模板。 ### 后端 (Django Rest Framework) 1. 创建一个 Django 项目和应用程序: ``` $ django-admin startproject myproject $ cd myproject $ python manage.py startapp myapp ``` 2. 安装 DRF: ``` $ pip install djangorestframework ``` 3. 在 `myproject/settings.py` 中添加 DRF 和 CORS 的配置: ```python INSTALLED_APPS = [ # ... 'rest_framework', ] MIDDLEWARE = [ # ... 'corsheaders.middleware.CorsMiddleware', 'django.middleware.common.CommonMiddleware', ] REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.SessionAuthentication', ), 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ), } CORS_ORIGIN_ALLOW_ALL = True ``` 4. 在 `myapp/views.py` 中定义一个视图: ```python from rest_framework.decorators import api_view from rest_framework.response import Response @api_view(['GET']) def hello(request): return Response({'message': 'Hello, world!'}) ``` 5. 在 `myproject/urls.py` 中添加路由: ```python from django.urls import path, include from myapp.views import hello urlpatterns = [ path('api/', include([ path('hello/', hello), ])), ] ``` 6. 运行服务器: ``` $ python manage.py runserver ``` 访问 `http://localhost:8000/api/hello/` 应该会返回 `{"message": "Hello, world!"}`。 ### 前端 (Vue 3 + Element Plus) 1. 使用 Vue CLI 创建一个新项目: ``` $ vue create myproject-frontend ``` 2. 安装 Element Plus: ``` $ npm install element-plus --save ``` 3. 在 `main.js` 中引入 Element Plus 和样式: ```javascript import { createApp } from 'vue' import App from './App.vue' import ElementPlus from 'element-plus' import 'element-plus/lib/theme-chalk/index.css' createApp(App) .use(ElementPlus) .mount('#app') ``` 4. 在 `App.vue` 中添加一个按钮和一个文本框: ```vue <template> <div class="container"> <el-input v-model="name" placeholder="Enter your name"></el-input> <el-button type="primary" @click="sayHello">Say hello</el-button> <div class="result">{{ result }}</div> </div> </template> <script> import axios from 'axios' export default { name: 'App', data() { return { name: '', result: '' } }, methods: { sayHello() { axios.get('/api/hello/', { headers: { 'Authorization': 'Token ' + sessionStorage.getItem('token') } }) .then(response => { this.result = response.data.message }) .catch(error => { console.error(error) }) } } } </script> <style> .container { max-width: 800px; margin: 0 auto; padding: 20px; } .result { margin-top: 20px; font-weight: bold; } </style> ``` 5. 运行服务器: ``` $ npm run serve ``` 访问 `http://localhost:8080/` 应该会显示一个文本框和一个按钮。在文本框中输入你的名字,然后点击按钮,应该会显示 `Hello, world!`。 这是一个简单的模板,你可以根据自己的需要进行扩展和定制。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值