从新建vue项目到引入组件Element流程

从新建vue项目到引入组件Element

以及Error when rendering component报错解决

一、新建项目

1.打开cmd,运行:vue init webpack Vue-Demo


2.运行:cd Vue-Demo进入这一级

3.运行:npm install

4.运行:npm run dev

如果浏览器打开之后,没有加载出页面,说明本地的8080 端口被占用,需要修改一下配置文件 config/index.js


解释:1.将 build 的路径前缀修改为 ' ./ '(原本为 ' / '),是因为打包之后,外部引入 js 和 css 文件时,如果路

径以 ' / ' 开头,那么在本地是无法找到对应文件。所以如果需要在本地打开打包后的文件,

就得修改文件路径。

2.将端口号改为不常用的端口。

显示页面如下:


项目新建完成。

二、引入Element

1.打开cmd,在当前目录中运行:npm i element-ui -S


2.src/main.js(红色的)

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-default/index.css'


Vue.config.productionTip = false

/* eslint-disable no-new */
Vue.use(ElementUI)

new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})

3.然后在.vue文件里就直接可以用了
例如:在src/components/Hello.vue做一下修改

<template>
  <div class="hello">
    <h1>{{ msg }}</h1>
    <h2>Essential Links</h2>
    <el-button>默认按钮</el-button>
    <el-button type="primary">主要按钮</el-button>
    <el-button type="text">文字按钮</el-button>
  </div>
</template>

<script>
export default {
  name: 'hello',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1, h2 {
  font-weight: normal;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  display: inline-block;
  margin: 0 10px;
}

a {
  color: #42b983;
}
</style>

运行:npm run dev

你将看到如下页面:

成功的引入了Element!!
更多样式请参考:http://element.eleme.io/#/zh-CN/component/layout

ps:
看了一些资料,有的引入之后会出现
报错:Error when rendering component
原因如下:
Vue 2.1.5 将 _h 重命名为 _c,而 Element 目前发的版本都是用以前的 compiler 编译的,
导致新版 runtime 无法运行 Element。目前的解决方案是锁定 Vue 的版本为 2.1.4
锁定vue相关版本
npm remove vue #卸载相关的版本
重新安装一下版本
"vue-template-compiler": "2.1.4"
"vue-loader": "10.0.0"
"vue": "2.1.4"

共勉~












  • 10
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
新建一个Vue项目引入Element UI的步骤如下: 1. 首先,确保你已经安装了Vue CLI和Node.js。Vue CLI可以通过在终端运行以下代码进行安装: ``` npm install -g @vue/cli ``` 安装完成后,你可以通过运行以下代码来验证是否安装成功: ``` vue --version ``` 同样,确保你已经安装了Node.js,你可以通过在终端运行以下代码来验证是否安装成功: ``` node --version ``` 2. 使用Vue CLI创建一个新的项目。打开终端,进入你想要创建项目的文件夹,并运行以下代码: ``` vue create 项目名 ``` 这将创建一个名为"项目名"的新的Vue项目。运行以上命令后,会提示你选择一些配置选项,你可以根据你的需要进行选择,或者直接按回车键选择默认选项。 安装完毕后,你可以通过以下命令进入项目文件夹: ``` cd 项目名 ``` 3. 安装Element UI。在终端中运行以下代码: ``` npm install element-ui ``` 4. 在main.js文件中引入Element UI。打开src文件夹,找到名为main.js的文件,并在文件开头添加以下代码: ```javascript import ElementUI from 'element-ui'; import 'element-ui/lib/theme-chalk/index.css'; Vue.use(ElementUI); ``` 5. 至此,你已经成功引入Element UI。你可以在项目的其他Vue文件中按照Element UI的文档使用它的组件,例如在组件引入Button组件: ```vue <template> <div> <el-button>按钮</el-button> </div> </template> <script> export default { name: 'MyComponent', // ... } </script> <style> /* ... */ </style> ``` 以上就是将Element UI引入Vue项目中的步骤。通过按照以上步骤操作,你可以使用Element UI的各种组件来构建你的Vue应用程序。
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值