vue(32) : win10创建vue2基础前端框架

这篇具有很好参考价值的文章主要介绍了vue(32) : win10创建vue2基础前端框架。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

  • vue2
  • element-ui
  • axios

含接口调用示例

1.创建vue2项目

开发工具为HBuilderX 3.7.3

1.1.新建项目

vue(32) : win10创建vue2基础前端框架,前端,vue.js,前端框架,javascript,vue项目2

1.2.普通项目-vue项目(2.6.10)

vue(32) : win10创建vue2基础前端框架,前端,vue.js,前端框架,javascript,vue项目2

等待创建项目

vue(32) : win10创建vue2基础前端框架,前端,vue.js,前端框架,javascript,vue项目2

2.安装element-ui组件

2.1右键左下角开始图标

vue(32) : win10创建vue2基础前端框架,前端,vue.js,前端框架,javascript,vue项目2

 2.2.cd进入项目目录,执行安装element-ui

npm i element-ui -S

vue(32) : win10创建vue2基础前端框架,前端,vue.js,前端框架,javascript,vue项目2

2.3.main.js引入配置

import {
  Pagination,
  Dialog,
  Autocomplete,
  Dropdown,
  DropdownMenu,
  DropdownItem,
  Menu,
  Submenu,
  MenuItem,
  MenuItemGroup,
  Input,
  InputNumber,
  Radio,
  RadioGroup,
  RadioButton,
  Checkbox,
  CheckboxButton,
  CheckboxGroup,
  Switch,
  Select,
  Option,
  OptionGroup,
  Button,
  ButtonGroup,
  Table,
  TableColumn,
  DatePicker,
  TimeSelect,
  TimePicker,
  Popover,
  Tooltip,
  Breadcrumb,
  BreadcrumbItem,
  Form,
  FormItem,
  Tabs,
  TabPane,
  Tag,
  Tree,
  Alert,
  Slider,
  Icon,
  Row,
  Col,
  Upload,
  Progress,
  Spinner,
  Badge,
  Card,
  Rate,
  Steps,
  Step,
  Carousel,
  CarouselItem,
  Collapse,
  CollapseItem,
  Cascader,
  ColorPicker,
  Transfer,
  Container,
  Header,
  Aside,
  Main,
  Footer,
  Timeline,
  TimelineItem,
  Link,
  Divider,
  Image,
  Calendar,
  Backtop,
  PageHeader,
  CascaderPanel,
  Loading,
  MessageBox,
  Message,
  Notification
} from 'element-ui';
 
Vue.use(Pagination);
Vue.use(Dialog);
Vue.use(Autocomplete);
Vue.use(Dropdown);
Vue.use(DropdownMenu);
Vue.use(DropdownItem);
Vue.use(Menu);
Vue.use(Submenu);
Vue.use(MenuItem);
Vue.use(MenuItemGroup);
Vue.use(Input);
Vue.use(InputNumber);
Vue.use(Radio);
Vue.use(RadioGroup);
Vue.use(RadioButton);
Vue.use(Checkbox);
Vue.use(CheckboxButton);
Vue.use(CheckboxGroup);
Vue.use(Switch);
Vue.use(Select);
Vue.use(Option);
Vue.use(OptionGroup);
Vue.use(Button);
Vue.use(ButtonGroup);
Vue.use(Table);
Vue.use(TableColumn);
Vue.use(DatePicker);
Vue.use(TimeSelect);
Vue.use(TimePicker);
Vue.use(Popover);
Vue.use(Tooltip);
Vue.use(Breadcrumb);
Vue.use(BreadcrumbItem);
Vue.use(Form);
Vue.use(FormItem);
Vue.use(Tabs);
Vue.use(TabPane);
Vue.use(Tag);
Vue.use(Tree);
Vue.use(Alert);
Vue.use(Slider);
Vue.use(Icon);
Vue.use(Row);
Vue.use(Col);
Vue.use(Upload);
Vue.use(Progress);
Vue.use(Spinner);
Vue.use(Badge);
Vue.use(Card);
Vue.use(Rate);
Vue.use(Steps);
Vue.use(Step);
Vue.use(Carousel);
Vue.use(CarouselItem);
Vue.use(Collapse);
Vue.use(CollapseItem);
Vue.use(Cascader);
Vue.use(ColorPicker);
Vue.use(Transfer);
Vue.use(Container);
Vue.use(Header);
Vue.use(Aside);
Vue.use(Main);
Vue.use(Footer);
Vue.use(Timeline);
Vue.use(TimelineItem);
Vue.use(Link);
Vue.use(Divider);
Vue.use(Image);
Vue.use(Calendar);
Vue.use(Backtop);
Vue.use(PageHeader);
Vue.use(CascaderPanel);
 
Vue.use(Loading.directive);
 
 
import axios from 'axios';
 
Vue.prototype.$loading = Loading.service;
Vue.prototype.$msgbox = MessageBox;
Vue.prototype.$alert = MessageBox.alert;
Vue.prototype.$confirm = MessageBox.confirm;
Vue.prototype.$prompt = MessageBox.prompt;
Vue.prototype.$notify = Notification;
Vue.prototype.$message = Message;
import 'element-ui/lib/theme-chalk/index.css';

3.安装axios

3.1.npm安装axios

npm install --save axios

vue(32) : win10创建vue2基础前端框架,前端,vue.js,前端框架,javascript,vue项目2

3.2.项目根目录创建[vue.config.js]文件, 内容如下

'use strict'
const path = require('path')
 
function resolve(dir) {
  return path.join(__dirname, dir)
}
 
const port = process.env.port || process.env.npm_config_port || 9527 // dev port
const name =  '测试' // page title
 
// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
  /**
   * You will need to set publicPath if you plan to deploy your site under a sub path,
   * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
   * then publicPath should be set to "/bar/".
   * In most cases please use '/' !!!
   * Detail: https://cli.vuejs.org/config/#publicpath
   */
  publicPath: '/',
  outputDir: 'dist',
  assetsDir: 'static',
  lintOnSave: process.env.NODE_ENV === 'development',
  productionSourceMap: false,
  devServer: {
    port: port,
    open: true,
    overlay: {
      warnings: false,
      errors: true
    },
    proxy: {
      // 代理test开头的uri
      '/test': {
        target: 'http://127.0.0.1:33001', // 后端地址
        // target: 'localhost:8080/manage', // 原始地址
        changeOrigin: true, // 开启代理,在本地创建一个虚拟服务端
        pathRewrite: {
          '^/test': 'test'
        }
      }
    }
    //before: require('./mock/mock-server.js')
  },
  configureWebpack: {
    // provide the app's title in webpack's name field, so that
    // it can be accessed in index.html to inject the correct title.
    name: name,
    resolve: {
      alias: {
        '@': resolve('src')
      }
    }
  },
  chainWebpack(config) {
    // it can improve the speed of the first screen, it is recommended to turn on preload
    // it can improve the speed of the first screen, it is recommended to turn on preload
    config.plugin('preload').tap(() => [
      {
        rel: 'preload',
        // to ignore runtime.js
        // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
        fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
        include: 'initial'
      }
    ])
 
    // when there are many pages, it will cause too many meaningless requests
    config.plugins.delete('prefetch')
 
    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()
  }
}

代理后端配置如下, 三个test改成相同的uri前缀即可, uri该签注会代理到后端

proxy: {
      // 代理test开头的uri
      '/test': {
        target: 'http://192.168.1.1:8080', // 后端地址
        changeOrigin: true, // 开启代理,在本地创建一个虚拟服务端
        pathRewrite: {
          '^/test': '/test'
        }
      }
    }

 3.3.src下创建util目录, util文件夹下创建request.js, 内容如下

const res = response.data

这个需要根据后端接口格式修改

/*引入axios*/
import axios from 'axios'
import {
	MessageBox,
	Message
} from 'element-ui'

const request = axios.create({
	baseURL: '', // 基础路径,将统一的部分全部封装
	withCredentials: true // 表示请求可以携带cookie
})

axios.defaults.headers['Content-Type'] = 'application/x-www-form-urlencoded'


// response interceptor
request.interceptors.response.use(
	/**
	 * If you want to get http information such as headers or status
	 * Please return  response => response
	 */

	/**
	 * Determine the request status by custom code
	 * Here is just an example
	 * You can also judge the status by HTTP Status Code
	 */
	response => {
		const res = response.data
		// if the custom code is not 20000, it is judged as an error.
		if (res.errCode !== 0) {
			Message({
				message: res.errMessage || 'Error',
				type: 'error',
				duration: 3 * 1000
			})
			// return Promise.reject(new Error(res.message || 'Error'))
			return null
		} else {
			return res
		}
	},
	error => {
		console.log('err' + error) // for debug
		Message({
			message: error.message,
			type: 'error',
			duration: 5 * 1000
		})
		return Promise.reject(error)
	}
)


//前端采用export.default,在写后端代码时用module.export
export default request

4.创建测试调用http

4.1.src下创建api目录, api下创建test.js, 内容如下

import request from '@/util/request'
 
 
export function test() {
  return request({
    url: '/test/2',
    method: 'get'
  })
}

4.2. App.vue替换

<template>
	<div>
		{{ testData }}
	</div>
</template>

<script>
	import {
		test,
	} from '@/api/test'

	export default {
		name: 'app',
		data() {
			return {
				testData: null
			}
		},
		created() {
			this.t1()
		},
		methods: {
			t1() {
				test().then(response => {
					console.log(response)
					this.testData = response.msg
				})
			}
		}
	}
</script>

<style>
</style>

4.3.后台接口需要返回以下内容

接口地址为 : http://127.0.0.1:33001/test/2

    @GetMapping("/test/2")
    public Object test2() {
        Map<String, Object> res = new HashMap<>();
        res.put("code", 200);
        res.put("errCode", 0);
        res.put("errMessage", "测试错误");
        res.put("msg", "SUCCESS");
        return res;
    }

内容如下

{
"msg": "SUCCESS",
"code": 200,
"errCode": 0,
"errMessage": "测试错误"

5.启动

npm run serve

vue(32) : win10创建vue2基础前端框架,前端,vue.js,前端框架,javascript,vue项目2

6.编译

npm run build

7.部署

dist里面的文件放到nginx目录下

nginx.conf添加以下配置

		location /test {
            proxy_pass http://127.0.0.1:33001;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   Host              $http_host;
            proxy_set_header   X-Real-IP         $remote_addr;
        }

可以参考 :VUE(5) : vue-element-admin[5] : 打包及nginx部署_使用nginx部署vue-element-admin项目-CSDN博客文章来源地址https://www.toymoban.com/news/detail-738857.html

到了这里,关于vue(32) : win10创建vue2基础前端框架的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • web前端框架JS学习之JavaScript类型转换

    web前端框架JS学习之JavaScript类型转换

    vascript有多种数据类型,如字符串、数字、布尔等,可以通过typeof语句来查看变量的数据类型。数据类型转换就是数据类型之间相互转换,比如把数字转成字符串、把布尔值转成字符串、把字符串转成数字等,这在工作也是经常碰到的。 本期我们就给大家说说web前端框架JS学

    2024年02月10日
    浏览(47)
  • 前端学习--vue2--2--vue指令基础

    前端学习--vue2--2--vue指令基础

    写在前面: 前置内容 - vue配置 vue指令只的是带有v-前缀的特殊标签属性 插值表达式{{}}是一种vue模板语法。 利用表达式进行差值,渲染到页面。 表达式可以是被求值的代码,js会计算 不能用差值表达式的 不存在的数据 {{data里面不存在的字段 js {{if}} 标签属性 span id=“

    2024年02月14日
    浏览(12)
  • ❤ 全面解析若依框架vue2版本(springboot-vue前后分离--前端部分)

    ❤ 全面解析若依框架vue2版本(springboot-vue前后分离--前端部分)

    一般在vue项目public文件夹下命名为“favicon.ico” ❤ 处理步骤 第1步:将图标重命名为“favicon.ico”,并放在项目根目录 下。 第2步:然后在index.html中引入,title中修改页面标题。 第3步:修改build文件夹下 webpack .dev.conf.js和webpack.prod.conf.js文件中的内容。 第五步:重新已经成功

    2024年02月12日
    浏览(52)
  • Vue2和Vue3是Vue.js框架的两个主要版本,它们之间有以下区别

    性能优化:Vue3在内部进行了重写,采用了更高效的虚拟DOM算法,使得渲染速度更快,性能更好。 更小的体积:Vue3的体积比Vue2更小,这意味着更快的下载和加载速度。 Composition API:Vue3引入了Composition API,它是一种新的组合式API,可以更好地组织和重用组件逻辑,使得代码更

    2024年02月15日
    浏览(15)
  • 【前端vue升级】vue2+js+elementUI升级为vue3+ts+elementUI plus

    【前端vue升级】vue2+js+elementUI升级为vue3+ts+elementUI plus

    gogo code 是一个基于 AST (源代码的抽象语法结构树状表现形式)的 JavaScript/Typescript/HTML 代码转换工具,可以用它来构建一个代码转换程序来帮助自动化完成如框架升级、代码重构、多平台转换等工作。 当前 GoGoCode 支持解析和操作如下类型的代码: ○JavaScript(JSX) ○Typescript

    2024年02月12日
    浏览(17)
  • 前端2023最全面试题(javaScript、typeScript、vue2、vue3、html、css、uniapp、webpack、vite、react)

    答案:JavaScript中的闭包是一种函数,它有权访问其词法环境的变量和其它函数。这意味着,即使其包含它的函数已经执行完毕,其词法环境仍然存在,因此可以访问其作用域内的变量。 答案:回调函数是在某个特定事件之后执行的函数。在JavaScript中,通常使用回调函数来处

    2024年02月06日
    浏览(13)
  • 【前端】在Vue2中使用Vanta.js炫酷动态背景(全屏背景)

    【前端】在Vue2中使用Vanta.js炫酷动态背景(全屏背景)

    官网:https://www.vantajs.com/ 由于博主在参考官网及官方GitHub进行应用时遇到一些问题,因此写了该篇博客,以避免大家因找Bug而浪费时间,方便快速的应用。 注意版本 Vue的版本如下 “vue”: “^2.6.14” 渲染容器 导包 方法 完整代码: 完整代码: 参加颜色参数 如果报错:‘X

    2024年02月11日
    浏览(15)
  • Jeecg开发框架前端VUE2数据页面与后端数据库交互实现

    Jeecg开发框架前端VUE2数据页面与后端数据库交互实现

    ​ JeecgBoot 是一款基于代码生成器的 低代码 开发平台,零代码开发!采用前后端分离架构:SpringBoot2.x,Ant DesignVue,Mybatis-plus,Shiro,JWT。强大的代码生成器让前后端代码一键生成,无需写任何代码! JeecgBoot引领新的开发模式(Online Coding模式- 代码生成器模式- 手工MERGE智能开发

    2024年02月11日
    浏览(33)
  • JavaScript 框架比较:Angular、React、Vue.js

    在 Web 开发领域,JavaScript 提供大量技术栈可供选择。其中最典型的三套组合,分别是 MERN、MEAN 和 MEVN。这些首字母相同的选项各自代表不同的技术加工具组合。为了在这些技术栈中做出明智选择,让我们先从核心组件聊起,再对各自前端框架(React、Angular 和 Vue)进行简化比

    2024年01月20日
    浏览(16)
  • Rspack 创建 vue2/3 项目接入 antdv(rspack.config.js 配置 less 主题)

    Rspack 创建 vue2/3 项目接入 antdv(rspack.config.js 配置 less 主题)

    Rspack CLI 官方文档。 rspack.config.js 官方文档。 创建项目(文档中还提供了 Rspack 内置 monorepo 框架 Nx 的创建方式,根据需求进行选择) 创建好项目并运行,目前 Rspack 版本支持的工程模版: 默认创建的 vue 项目为 vue3 : 如果需要其他版本,或其他框架的基础工程,可到官方提

    2024年02月11日
    浏览(13)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包