在vue中结合render函数渲染指定的组件到容器中
1、demo
项目结构:

index.html
<!DOCTYPE html>
<html>
<head>
<title>标题</title>
<meta charset="utf-8">
</head>
<body>
<h3 class="blue">h3标签内的文本</h3>
<ul>
<li>这是一个li标签</li>
<li>这是一个li标签</li>
<li>这是一个li标签</li>
<li>这是一个li标签</li>
<li>这是一个li标签</li>
</ul> <div class="img"></div> <div id="app">
<h3> {{ msg }} </h3> <login></login>
</div> <script type="text/javascript" src="../dist/bundle.js"></script>
</body>
</html>
main.js
// 这是项目的入口js文件
// 导入jquery, 这是ES6中导入模块的语法
import $ from 'jquery';
import './css/common.css'; // 注意:在webpack中,使用下面这句导入vue,功能不完整,只提供runtime-only的方式,并没没有提供向网页中那样的使用方式
//import Vue from 'vue';
// 导入完整的vue
import Vue from '../node_modules/vue/dist/vue.js'; // 导入register组件
import register from './components/register.vue';
// 默认webpack无法处理.vue文件,要安装第三方loader
// npm i vue-loader vue-template-compiler -D $(function() {
$('li:odd').css('backgroundColor','yellow');
$('li:even').css('backgroundColor','#eee');
}); // 组件的模板对象
var login = {
template: '<h1>登录组件</h1>'
}; var vm = new Vue({
el: '#app',
data: {
msg: 'hello vue'
},
components: {
login
},
// render:将register组件渲染到el指定的div#app中(div#app中其他内容被覆盖)
// 简写render: c => c(register)
render: function (createElements) {
return createElements(register);
} });
register.vue
<template>
<h1>注册组件</h1>
</template> <script>
</script> <style>
</style>
package.json
{
"name": "webpack-study1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"jquery": "^3.3.1",
"vue": "^2.6.10"
},
"devDependencies": {
"css-loader": "^0.28.0",
"file-loader": "^1.1.11",
"style-loader": "^0.21.0",
"url-loader": "^1.0.1",
"vue-loader": "^15.2.4",
"vue-template-compiler": "^2.5.16",
"webpack": "^3.6.0"
}
}
webpack.config.js
var path = require('path');
const { VueLoaderPlugin } = require('vue-loader');
// 通过node的模块操作,向外暴露一个配置对象
module.exports = {
entry: path.join(__dirname, './src/main.js'), // 入口,表示要使用webpack打包哪个文件
output: { // 出口
path: path.join(__dirname, './dist'),
filename: 'bundle.js'
},
module: { //用于配置所有第三方模块加载器
rules: [ //所有第三方模块的匹配规则
// 首先安装loader: npm i style-loader css-loader -D, 然后配置
{ test:/\.css$/, use: ['style-loader','css-loader'] },
// 首先安装loader: npm i url-loader file-loader -D, 然后配置
{
test:/\.(jpg|png|gif|bmp|jpeg)$/,
use: ['url-loader']
// limit单位byte,如果图片大于等于limit给定值,则不会被转为base64格式字符串
// name=[name].[ext]:打包的图片原名相同
// use: ['url-loader?limit=1000&name=[name].[ext]']
},
// 首先安装loader: npm i vue-loader vue-template-compiler -D, 然后配置
{ test: /\.vue$/, use: 'vue-loader' }
]
},
plugins: [
new VueLoaderPlugin()
],
};
2、遇到的问题
2.1、webpack版本是3.6.0,与css-loader不兼容


2.2、下载安装vue-loader以后,报错缺少相关插件 include VueLoaderPlugin in your webpack config
const { VueLoaderPlugin } = require('vue-loader');
……
plugins: [
new VueLoaderPlugin(),
],
在vue中结合render函数渲染指定的组件到容器中的更多相关文章
- render 函数渲染表格的当前数据列使用
columns7: [ { title: '编号', align: 'center', width: 90, key: 'No', render: (h, params) => { return ...
- iview table表中使用render函数props传值出现问题
使用iview中的table表格时避免不了使用render函数渲染自定义内容,或者渲染组件.但是在正常使用时出现了props传值无法识别, 按照官网介绍使用props如下: render: (h, p ...
- 使用render函数渲染组件
使用render函数渲染组件:https://www.jianshu.com/p/27ec4467a66b
- iview table中的render函数使用
1.表格列数据内容过多可以用以下两个属性解决: ellipsis:"true', tooltip:true 使每个列的内容如果过多的话变为省略号 2.table中的render函数(实现根据 ...
- vue中的render函数介绍
简介:对于不了解slot的用法(参考:大白话vue-slot的用法)又刚接触render函数的同学来说,官网的解释无疑一脸懵逼,这里就整理下个人对render函数的理解 问题: 1.render函数是 ...
- Vue中的render函数随笔
使用vue-cli创建项目后,再main.js里面有这样一段代码: new Vue({ render:h => h(App) }).$mount('#app') render函数是渲染一个视图, ...
- Vue.js之render函数基础
刚才翻了一下博客,才发现,距离自己写的第一篇Vue的博客vue.js之绑定class和style(2016-10-30)已经过去一年零两天.这一年里,自己从船厂的普通技术员,成为了一个微型不靠谱创业公 ...
- 19 使用Vue实例的render方法渲染组件
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Vue2.x中的Render函数
Render函数是Vue2.x版本新增的一个函数:使用虚拟dom来渲染节点提升性能,因为它是基于JavaScript计算.通过使用createElement(h)来创建dom节点.createElem ...
随机推荐
- react 中 EventEmitter 事件总线机制
此机制可用于 react 中兄弟组件中的通信 npm install events -S 事件总线: // eventBus.js import {EventEmitter} from 'events ...
- [转帖]浅谈IOC--说清楚IOC是什么
浅谈IOC--说清楚IOC是什么 Need Study https://www.cnblogs.com/DebugLZQ/archive/2013/06/05/3107957.html 博文目录 1. ...
- python nonlocal 的具体原理
很多文章都大概列了下nonlocal的具体用法,比如看到几篇文章写的 “nonlocal关键字用来在函数或其他作用域中使用外层(非全局)变量” 看完以后我感觉自己是懂了,但光从这句话来说还没完全理解它 ...
- Maven - Maven3实战学习笔记(3)使用maven构建Web应用
1.jetty-maven-plugin自动化测试Web应用工具 <plugin> <groupId>org.mortbay.jetty</groupId> < ...
- 一台 Java 服务器可以跑多少个线程?
一台Java服务器能跑多少个线程? 这个问题来自一次线上报警如下图,超过了我们的配置阈值. 京东自研UMP监控分析 打出jstack文件,通过IBM Thread and Monitor Dump A ...
- centos 防火墙 iptables firewalld SELinux
参考 Centos7 只启用iptables 禁用firewalld功能 java.net.NoRouteToHostException: 没有到主机的路由 相关内容 centos7 中才开始引用fi ...
- VS Code Python 全新发布!Jupyter Notebook 原生支持终于来了!
VS Code Python 全新发布!Jupyter Notebook 原生支持终于来了! 北京时间 2019 年 10 月 9 日,微软发布了全新的 VS Code Python 插件,带来了众多 ...
- python3:tuple元组
https://www.runoob.com/python3/python3-tuple.html 元组使用小括号,列表使用方括号. 元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. Py ...
- python实现建造者模式
python实现建造者模式 前言 无论是在现实世界中还是在软件系统中,都存在一些复杂的对象,它们拥有多个组成部分,如汽车,它包括车轮.方向盘.发送机等各种部件.而对于大多数用户而言,无须知道这些部件的 ...
- 62. Unique Paths (JAVA)
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The ...