File tree Expand file tree Collapse file tree 4 files changed +53
-8
lines changed
Expand file tree Collapse file tree 4 files changed +53
-8
lines changed Original file line number Diff line number Diff line change 99 margin : 0 ;
1010 }
1111
12- .icon {
13- display : inline-block;
14- line-height : 0 ;
15- }
1612
17- .icon > svg {
18- stroke : currentColor;
19- }
2013 </ style >
2114</ head >
2215< body >
Original file line number Diff line number Diff line change 99 "babel-loader" : " ^6.2.10" ,
1010 "babel-preset-es2015" : " ^6.22.0" ,
1111 "css-loader" : " ^0.26.1" ,
12+ "stylus" : " ^0.54.5" ,
13+ "stylus-loader" : " ^2.4.0" ,
1214 "vue" : " ^2.1.10" ,
1315 "vue-loader" : " ^10.1.2" ,
1416 "vue-template-compiler" : " ^2.1.10" ,
Original file line number Diff line number Diff line change 11<template >
2- <div >Hello World </div >
2+ <div >< icon name = " square " size = " 48 " ></ icon > </div >
33</template >
44
55<script >
66 import {mapState } from ' vuex' ;
7+ import Icon from ' ./Icon' ;
78
89 export default {
910 name: ' App' ,
Original file line number Diff line number Diff line change 1+ <template >
2+ <div class =" icon" ></div >
3+ </template >
4+
5+ <script >
6+ export default {
7+ name: ' Icon' ,
8+ props: {
9+ name: {
10+ type: String ,
11+ required: true
12+ },
13+ size: {
14+ type: String ,
15+ default: ' 24'
16+ }
17+ },
18+ mounted () {
19+ fetch (` ./icons/${ this .name } .svg` )
20+ .then (response => {
21+ if (response .ok ) {
22+ return response .text ();
23+ }
24+ throw new Error (` Cannot find ${ this .name } .svg` );
25+ })
26+ .then (svgText => {
27+ const svgDocument = new DOMParser ().parseFromString (svgText, ' image/svg+xml' );
28+ const svgIcon = svgDocument .querySelector (' svg' ).cloneNode (true );
29+
30+ svgIcon .setAttribute (' width' , this .size );
31+ svgIcon .setAttribute (' height' , this .size );
32+
33+ this .$el .appendChild (svgIcon);
34+ })
35+ .catch (error => {
36+ console .error (error);
37+ });
38+ }
39+ }
40+ </script >
41+
42+ <style lang="stylus">
43+ .icon
44+ display inline-block
45+ line-height 0
46+
47+ .icon > svg
48+ stroke currentColor
49+ </style >
You can’t perform that action at this time.
0 commit comments