-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-render-demo.html
62 lines (62 loc) · 1.42 KB
/
vue-render-demo.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Components Example</title>
<script src="./vue.js"></script>
</head>
<body>
<div id="app" class="demo">
<anchored-heading :level="1">
<div>
我是h1
<div>
我是h2
<div>我是h3</div>
</div>
</div>
</anchored-heading>
</div>
<script>
const getNodeText = (nodes) => {
return nodes
.map((node) => {
return node.children ? getNodeText(node.children) : node.text;
})
.join("");
};
Vue.component("anchored-heading", {
props: {
level: {
type: Number,
required: true,
},
},
render: function (createElement) {
const headingId = getNodeText(this.$slots.default)
.replace(/\W+/g, "-")
.replace(/(^-|-$)/g, "");
const tag = "h" + this.level;
const children = [
createElement(
"a",
{
attrs: {
name: headingId,
href: "#" + headingId,
},
},
this.$slots.default
),
];
return createElement(tag, children);
},
});
const data = {};
new Vue({
el: "#app",
data,
computed: {},
});
</script>
</body>
</html>