反正你们写的都是没有即时演示的教程,我为什么不去看官方文档?
Posted conmajia
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了反正你们写的都是没有即时演示的教程,我为什么不去看官方文档?相关的知识,希望对你有一定的参考价值。
我就拿Vue举个栗子。
Vue.js是什么,我想你多半知道,不然你也不会打开这篇文章。
这几天看到写Vue教程的文章突然多起来了。不过,就我所见,在这里发表的所有教程文章,都不支持即时演示,顶多有点儿自己画的图片之类的点缀一下。那么,请问您的文章和官网的交互式教程相比有什么优势?高级技巧或者独创的tricks就算了,那些写基础教程的作者们,您的目标读者全是不看官方文档,吃不下饼干非得您给嚼碎了再喂的那种蠢驴吗?
但是!
看完这篇文章,你至少可以在你的教程里加入一些简单的Vue交互(或者别的骚东西)!就像这个被我搬过来的官方例子一样:
▲ 点一下按钮
new Vue({
el: ‘#list-complete-demo‘,
data: {
items: [1,2,3,4,5,6,7,8,9],
nextNum: 10
},
methods: {
randomIndex: function () {
return Math.floor(Math.random() * this.items.length)
},
add: function () {
this.items.splice(this.randomIndex(), 0, this.nextNum++)
},
remove: function () {
this.items.splice(this.randomIndex(), 1)
}
}
})
几个关键
给文章加上interactive内容,尤其是在第三方平台(比如BKY)发表的文章,没有插件功能,必须用到html权限。但是文章内的<script>
已经被屏蔽了,直接写是不会解析的。所以为了让文章能交互,你得绕过script
的屏蔽。
简易代码执行器
我在《如何绕过BKY对 script 的屏蔽》里提供了一个简易的解决方案,我称为简易代码执行器(ECE)。就目前的效果而言,正如你在这篇文章里看到的,它的效果还挺好的,至少够用了。通过每篇文章各自执行代码,你可以省去在全局设置里不断添加新代码的烦恼。相信我,当你写的文章越来越多,global maintenance会把你逼疯的。等到哪天不用这些代码了,直接删除文章就好,免去了在狭小的设置对话框的犄角旮旯里寻找作废代码的烦恼。
ECE会读取文章里所有可识别的代码块,然后分别执行它们。对我这个博客来说,一个标准的文章内嵌代码块是用 run
或者 runearly
标签包围的代码块:
<run>
<!-- some code -->
</run>
文章加载完毕后,ECE就会开始逐个执行代码块里的some code
。拿一个简单的Vue官方代码举例:
{{ message }}
var app5 = new Vue({
el: ‘#app-5‘,
data: {
message: ‘Hello Vue.js!‘
},
methods: {
reverseMessage: function () {
this.message = this.message.split(‘‘).reverse().join(‘‘)
}
}
})
普通的博客文章(就是我吐槽的那些基础教程)是没法做到上面这样真正使用Vue.js实现的交互效果的。如果不用别的招,它们只能在文章里干瘪地罗列出一堆代码,读者看了半天也是满头雾水,依然不知道这玩意儿到底是个什么样。有心的读者也许会按图索骥,摸到官网去看文档和交互演示,心中满是问号:为什么不从一开始就直接看官网。而意志薄弱的哥们儿,可能就此放弃阅读,关掉网页,顺便再骂上一句:辣鸡文章。
如果用了内嵌代码块这种methodology,只需要在文章正文写上HTML代码:
<div id="app-5">
<p>{{ message }}</p>
<button v-on:click="reverseMessage">逆转消息</button>
</div>
然后把Vue语句写到一个run
标签里
<run>
var app5 = new Vue({
el: '#app-5',
data: {
message: 'Hello Vue.js!'
},
methods: {
reverseMessage: function() {
this.message = this.message.split('').reverse().join('')
}
}
})
</run>
文章正文内容加载完后,ECE会执行这些Vue代码,就能得到上面的交互效果。
简易代码执行器的实现很简单,它分为标准执行阶段和推迟执行阶段。由 runearly
包裹的代码块在DOM加载完毕后就开始执行,相当于:
$(function() {
/* 代码 */
});
run
里的代码需要等到文档全部载入后才执行,相当于:
$(window).load(function() {
/* 代码 */
});
这很好理解,如果想不明白,看看我前面提到的那篇文章。
多个代码块之间传递变量
我的代码是用eval
执行的,这带来一个问题:多个run
代码块之间没法直接传递变量。比如我写:
<run>
var a = 1;
</run>
<run>
a = a + 1;
</run>
第二个run
是没法执行的,浏览器会报错:
ReferenceError: a is not defined
a
的生命周期只限于第一个run
内。为了解决这个问题,我在全局设置里增加了一个缓存变量和三个助手函数,分别用来读取/写入/删除需要共享的变量:
var steady = new Array();
function getValue(name) {
return steady[name];
}
function setValue(name, value) {
steady[name] = value;
}
function removeValue(name) {
steady.remove(name);
}
steady
因为是写在全局设置里的,它的生命周期是整个session。通过它,各个代码块之间就可以传递变量了,这样,你在写文章尤其是长文章时就能把代码块分开写在文章不同的位置,而不用把所有的代码都挤在一个run
里,影响写作思路。
另外一种变量传递解决方案是使用cookie完成。Cookie优点在于可以在不同页面间传递值,这一点上面的方案是办不到的。但是它的缺点也和优点一样明显。Cookie没办法直接保存除了字符串和数字之外的变量,比如数组。好在大部分网页内容和变量都可以通过序列化转换成JSON字符串进行保存/读取。Cookie的操作可以借用js-cookie完成,非常方便。
其他的骚操作
有了运行代码块的能力,你可以在讲解Vue的时候,引入其他的内容配合讲解。比如把Vue和ECharts结合起来,实现图文并茂的讲解和阐述。轻轻松松就可以画出精美的交互式图表,再搭配Vue的动态绑定插槽等等。这样装修之后文章自然完爆那些用着默认主题,呆板、无神的鸡肋文。下面这两张图都是可以点的,试试看:
function paintCanvas(canvas) {
var myChart = echarts.init(document.getElementById(canvas));
myChart.showLoading();
var json_data = {
"name": "flare",
"children": [{
"name": "analytics",
"children": [{
"name": "cluster",
"children": [{
"name": "AgglomerativeCluster",
"value": 3938
}, {
"name": "CommunityStructure",
"value": 3812
}, {
"name": "HierarchicalCluster",
"value": 6714
}, {
"name": "MergeEdge",
"value": 743
}]
}, {
"name": "graph",
"children": [{
"name": "BetweennessCentrality",
"value": 3534
}, {
"name": "LinkDistance",
"value": 5731
}, {
"name": "MaxFlowMinCut",
"value": 7840
}, {
"name": "ShortestPaths",
"value": 5914
}, {
"name": "SpanningTree",
"value": 3416
}]
}, {
"name": "optimization",
"children": [{
"name": "AspectRatioBanker",
"value": 7074
}]
}]
}, {
"name": "animate",
"children": [{
"name": "Easing",
"value": 17010
}, {
"name": "FunctionSequence",
"value": 5842
}, {
"name": "interpolate",
"children": [{
"name": "ArrayInterpolator",
"value": 1983
}, {
"name": "ColorInterpolator",
"value": 2047
}, {
"name": "DateInterpolator",
"value": 1375
}, {
"name": "Interpolator",
"value": 8746
}, {
"name": "MatrixInterpolator",
"value": 2202
}, {
"name": "NumberInterpolator",
"value": 1382
}, {
"name": "ObjectInterpolator",
"value": 1629
}, {
"name": "PointInterpolator",
"value": 1675
}, {
"name": "RectangleInterpolator",
"value": 2042
}]
}, {
"name": "ISchedulable",
"value": 1041
}, {
"name": "Parallel",
"value": 5176
}, {
"name": "Pause",
"value": 449
}, {
"name": "Scheduler",
"value": 5593
}, {
"name": "Sequence",
"value": 5534
}, {
"name": "Transition",
"value": 9201
}, {
"name": "Transitioner",
"value": 19975
}, {
"name": "TransitionEvent",
"value": 1116
}, {
"name": "Tween",
"value": 6006
}]
}, {
"name": "data",
"children": [{
"name": "converters",
"children": [{
"name": "Converters",
"value": 721
}, {
"name": "DelimitedTextConverter",
"value": 4294
}, {
"name": "GraphMLConverter",
"value": 9800
}, {
"name": "IDataConverter",
"value": 1314
}, {
"name": "JSONConverter",
"value": 2220
}]
}, {
"name": "DataField",
"value": 1759
}, {
"name": "DataSchema",
"value": 2165
}, {
"name": "DataSet",
"value": 586
}, {
"name": "DataSource",
"value": 3331
}, {
"name": "DataTable",
"value": 772
}, {
"name": "DataUtil",
"value": 3322
}]
}, {
"name": "display",
"children": [{
"name": "DirtySprite",
"value": 8833
}, {
"name": "LineSprite",
"value": 1732
}, {
"name": "RectSprite",
"value": 3623
}, {
"name": "TextSprite",
"value": 10066
}]
}, {
"name": "flex",
"children": [{
"name": "FlareVis",
"value": 4116
}]
}, {
"name": "physics",
"children": [{
"name": "DragForce",
"value": 1082
}, {
"name": "GravityForce",
"value": 1336
}, {
"name": "IForce",
"value": 319
}, {
"name": "NBodyForce",
"value": 10498
}, {
"name": "Particle",
"value": 2822
}, {
"name": "Simulation",
"value": 9983
}, {
"name": "Spring",
"value": 2213
}, {
"name": "SpringForce",
"value": 1681
}]
}, {
"name": "query",
"children": [{
"name": "AggregateExpression",
"value": 1616
}, {
"name": "And",
"value": 1027
}, {
"name": "Arithmetic",
"value": 3891
}, {
"name": "Average",
"value": 891
}, {
"name": "BinaryExpression",
"value": 2893
}, {
"name": "Comparison",
"value": 5103
}, {
"name": "CompositeExpression",
"value": 3677
}, {
"name": "Count",
"value": 781
}, {
"name": "DateUtil",
"value": 4141
}, {
"name": "Distinct",
"value": 933
}, {
"name": "Expression",
"value": 5130
}, {
"name": "ExpressionIterator",
"value": 3617
}, {
"name": "Fn",
"value": 3240
}, {
"name": "If",
"value": 2732
}, {
"name": "IsA",
"value": 2039
}, {
"name": "Literal",
"value": 1214
}, {
"name": "Match",
"value": 3748
}, {
"name": "Maximum",
"value": 843
}, {
"name": "methods",
"children": [{
"name": "add",
"value": 593
}, {
"name": "and",
"value": 330
}, {
"name": "average",
"value": 287
}, {
"name": "count",
"value": 277
}, {
"name": "distinct",
"value": 292
}, {
"name": "div",
"value": 595
}, {
"name": "eq",
"value": 594
}, {
"name": "fn",
"value": 460
}, {
"name": "gt",
"value": 603
}, {
"name": "gte",
"value": 625
}, {
"name": "iff",
"value": 748
}, {
"name": "isa",
"value": 461
}, {
"name": "lt",
"value": 597
}, {
"name": "lte",
"value": 619
}, {
"name": "max",
"value": 283
}, {
"name": "min",
"value": 283
}, {
"name": "mod",
"value": 591
}, {
"name": "mul",
"value": 603
}, {
"name": "neq",
"value": 599
}, {
"name": "not",
"value": 386
}, {
"name": "or",
"value": 323
}, {
"name": "orderby",
"value": 307
}, {
"name": "range",
"value": 772
}, {
"name": "select",
"value": 296
}, {
"name": "stddev",
"value": 363
}, {
"name": "sub",
"value": 600
}, {
"name": "sum",
"value": 280
}, {
"name": "update",
"value": 307
}, {
"name": "variance",
"value": 335
}, {
"name": "where",
"value": 299
}, {
"name": "xor",
"value": 354
}, {
"name": "-",
"value": 264
}]
}, {
"name": "Minimum",
"value": 843
}, {
"name": "Not",
"value": 1554
}, {
"name": "Or",
"value": 970
}, {
"name": "Query",
"value": 13896
}, {
"name": "Range",
"value": 1594
}, {
"name": "StringUtil",
"value": 4130
}, {
"name": "Sum",
"value": 791
}, {
"name": "Variable",
"value": 1124
}, {
"name": "Variance",
"value": 1876
}, {
"name": "Xor",
"value": 1101
}]
}, {
"name": "scale",
"children": [{
"name": "IScaleMap",
"value": 2105
}, {
"name": "LinearScale",
"value": 1316
}, {
"name": "LogScale",
"value": 3151
}, {
"name": "OrdinalScale",
"value": 3770
}, {
"name": "QuantileScale",
"value": 2435
}, {
"name": "QuantitativeScale",
"value": 4839
}, {
"name": "RootScale",
"value": 1756
}, {
"name": "Scale",
"value": 4268
}, {
"name": "ScaleType",
"value": 1821
}, {
"name": "TimeScale",
"value": 5833
}]
}, {
"name": "util",
"children": [{
"name": "Arrays",
"value": 8258
}, {
"name": "Colors",
"value": 10001
}, {
"name": "Dates",
"value": 8217
}, {
"name": "Displays",
"value": 12555
}, {
"name": "Filter",
"value": 2324
}, {
"name": "Geometry",
"value": 10993
}, {
"name": "heap",
"children": [{
"name": "FibonacciHeap",
"value": 9354
}, {
"name": "HeapNode",
"value": 1233
}]
}, {
"name": "IEvaluable",
"value": 335
}, {
"name": "IPredicate",
"value": 383
}, {
"name": "IValueProxy",
"value": 874
}, {
"name": "math",
"children": [{
"name": "DenseMatrix",
"value": 3165
}, {
"name": "IMatrix",
"value": 2815
}, {
"name": "SparseMatrix",
"value": 3366
}]
}, {
"name": "Maths",
"value": 17705
}, {
"name": "Orientation",
"value": 1486
}, {
"name": "palette",
"children": [{
"name": "ColorPalette",
"value": 6367
}, {
"name": "Palette",
"value": 1229
}, {
"name": "ShapePalette",
"value": 2059
}, {
"name": "SizePalette",
"value": 2291
}]
}, {
"name": "Property",
"value": 5559
}, {
"name": "Shapes",
"value": 19118
}, {
"name": "Sort",
"value": 6887
}, {
"name": "Stats",
"value": 6557
}, {
"name": "Strings",
"value": 22026
}]
}, {
"name": "vis",
"children": [{
"name": "axis",
"children": [{
"name": "Axes",
"value": 1302
}, {
"name": "Axis",
"value": 24593
}, {
"name": "AxisGridLine",
"value": 652
}, {
"name": "AxisLabel",
"value": 636
}, {
"name": "CartesianAxes",
"value": 6703
}]
}, {
"name": "controls",
"children": [{
"name": "AnchorControl",
"value": 2138
}, {
"name": "ClickControl",
"value": 3824
}, {
"name": "Control",
"value": 1353
}, {
"name": "ControlList",
"value": 4665
}, {
"name": "DragControl",
"value": 2649
}, {
"name": "ExpandControl",
"value": 2832
}, {
"name": "HoverControl",
"value": 4896
}, {
"name": "IControl",
"value": 763
}, {
"name": "PanZoomControl",
"value": 5222
}, {
"name": "SelectionControl",
"value": 7862
}, {
"name": "TooltipControl",
"value": 8435
}]
}, {
"name": "data",
"children": [{
"name": "Data",
"value": 20544
}, {
"name": "DataList",
"value": 19788
}, {
"name": "DataSprite",
"value": 10349
}, {
"name": "EdgeSprite",
"value": 3301
}, {
"name": "NodeSprite",
"value": 19382
}, {
"name": "render",
"children": [{
"name": "ArrowType",
"value": 698
}, {
"name": "EdgeRenderer",
"value": 5569
}, {
"name": "IRenderer",
"value": 353
}, {
"name": "ShapeRenderer",
"value": 2247
}]
}, {
"name": "ScaleBinding",
"value": 11275
}, {
"name": "Tree",
"value": 7147
}, {
"name": "TreeBuilder",
"value": 9930
}]
}, {
"name": "events",
"children": [{
"name": "DataEvent",
"value": 2313
}, {
"name": "SelectionEvent",
"value": 1880
}, {
"name": "TooltipEvent",
"value": 1701
}, {
"name": "VisualizationEvent",
"value": 1117
}]
}, {
"name": "legend",
"children": [{
"name": "Legend",
"value": 20859
}, {
"name": "LegendItem",
"value": 4614
}, {
"name": "LegendRange",
"value": 10530
}]
}, {
"name": "operator",
"children": [{
"name": "distortion",
"children": [{
"name": "BifocalDistortion",
"value": 4461
}, {
"name": "Distortion",
"value": 6314
}, {
"name": "FisheyeDistortion",
"value": 3444
}]
}, {
"name": "encoder",
"children": [{
"name": "ColorEncoder",
"value": 3179
}, {
"name": "Encoder",
"value": 4060
}, {
"name": "PropertyEncoder",
"value": 4138
}, {
"name": "ShapeEncoder",
"value": 1690
}, {
"name": "SizeEncoder",
"value": 1830
}]
}, {
"name": "filter",
"children": [{
"name": "FisheyeTreeFilter",
"value": 5219
}, {
"name": "GraphDistanceFilter",
"value": 3165
}, {
"name": "VisibilityFilter",
"value": 3509
}]
}, {
"name": "IOperator",
"value": 1286
}, {
"name": "label",
"children": [{
"name": "Labeler",
"value": 9956
}, {
"name": "RadialLabeler",
"value": 3899
}, {
"name": "StackedAreaLabeler",
"value": 3202
}]
}, {
"name": "layout",
"children": [{
"name": "AxisLayout",
"value": 6725
}, {
"name": "BundledEdgeRouter",
"value": 3727
}, {
"name": "CircleLayout",
"value": 9317
}, {
"name": "CirclePackingLayout",
"value": 12003
}, {
"name": "DendrogramLayout",
"value": 4853
}, {
"name": "ForceDirectedLayout",
"value": 8411
}, {
"name": "IcicleTreeLayout",
"value": 4864
}, {
"name": "IndentedTreeLayout",
"value": 3174
}, {
"name": "Layout",
"value": 7881
}, {
"name": "NodeLinkTreeLayout",
"value": 12870
}, {
"name": "PieLayout",
"value": 2728
}, {
"name": "RadialTreeLayout",
"value": 12348
}, {
"name": "RandomLayout",
"value": 870
}, {
"name": "StackedAreaLayout",
"value": 9121
}, {
"name": "TreeMapLayout",
"value": 9191
}]
}, {
"name": "Operator",
"value": 2490
}, {
"name": "OperatorList",
"value": 5248
}, {
"name": "OperatorSequence",
"value": 4190
}, {
"name": "OperatorSwitch",
"value": 2581
}, {
"name": "SortOperator",
"value": 2023
}]
}, {
"name": "Visualization",
"value": 16540
}]
}]
};
myChart.hideLoading();
myChart.setOption(option = {
tooltip: {
trigger: ‘item‘,
triggerOn: ‘mousemove‘
},
series: [{
type: ‘tree‘,
data: [json_data],
top: ‘18%‘,
bottom: ‘14%‘,
layout: ‘radial‘,
symbol: ‘emptyCircle‘,
symbolSize: 7,
initialTreeDepth: 3,
animationDurationUpdate: 750
}]
});
}
paintCanvas(‘chart1‘);
或者为你强大的项目画个错综复杂秀破天际的类关系图:
function paintCanvas(canvas,data,title=‘‘){var chart=echarts.init(document.getElementById(canvas));chart.setOption({title:{text:title},tooltip:{},legend:{data:[‘‘]},dataZoom:[{id:‘dataZoomX‘,type:‘slider‘,xAxisIndex:[0],filterMode:‘filter‘}],xAxis:{type:‘category‘,data:data.x},yAxis:{scale:true},series:[{name:‘‘,type:‘bar‘,data:data.y}],grid:{top:10}})}
paintCanvas(‘chart2‘,{x:[0,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,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99],y:[0.009999,0.0099791,0.0099851,0.010045,0.0099323,0.0100227,0.009973,0.0099823,0.0099812,0.0100117,0.0099555,0.0100219,0.0099808,0.0099235,0.0100256,0.0100195,0.0100347,0.0099649,0.0099957,0.0100012,0.0099983,0.0100443,0.0100185,0.0100028,0.0099762,0.0099998,0.010038,0.0099765,0.0100109,0.01005,0.01003,0.0100054,0.0099526,0.010001,0.0100172,0.0100067,0.0099779,0.0100547,0.0100519,0.009982,0.0100288,0.0100559,0.0100406,0.0099738,0.009963,0.0099715,0.0099753,0.0099745,0.0099862,0.0099928,0.0100211,0.009992,0.0100048,0.0099731,0.0100005,0.0100157,0.0100208,0.0099976,0.0099595,0.01004,0.0100246,0.0100253,0.0100169,0.0099769,0.0099607,0.0100206,0.010013,0.0099873,0.0099567,0.0099987,0.0099625,0.0100595,0.0099338,0.0100009,0.0100181,0.0099867,0.0100141,0.010015,0.0099953,0.0100089,0.0100287,0.0100257,0.0100045,0.0100001,0.0100012,0.0100357,0.0099458,0.0100448,0.0099926,0.0099496,0.0100401,0.0099849,0.0099666,0.0100041,0.0100289,0.0099873,0.009978,0.0099922,0.0099923,0.0100045]});
反正在网上写文章这种行为已经没什么节操可言了,为什么不用你脑中那些乱七八糟的JS知识,来让你的文章更骚一点呢?是你吗,秀儿?
本该耀眼的文章,你怎么好意思让它丑陋不堪,寡淡无味,泯然于众?!
The End. (Box)
以上是关于反正你们写的都是没有即时演示的教程,我为什么不去看官方文档?的主要内容,如果未能解决你的问题,请参考以下文章