Home>Article>Web Front-end> taro-script 0.4 released, learn about the js interpreter component based on Taro v3
Related learning recommendations:js video tutorial
Github address
Based onTaro v3
development, supports multi-terminal small programs to dynamically load remote JavaScript scripts and execute them,supports ES5 syntax
useScriptContext
Get the current execution contexttext
Attribute, you can directly pass in the js stringsrc
Supports arrays to solve the multi-layer TaroScript nesting problemnpm install --save taro-script复制代码
import TaroScript from "taro-script";;复制代码
import TaroScript from "taro-script";;复制代码 Hello TaroScript
Note 1: The sametaro-script
will only be executed once, that is, aftercomponentDidMount
. Subsequent changes to the properties will be invalid. Example
function App({ url }) { // 只会在第一次创建后加载并执行,后续组件的更新会忽略所有属性变动 return; }复制代码
Note 2: Multipletaro-script
will be loaded in parallel and executed out of order, and the order cannot be guaranteed. For example:
// 并行加载及无序执行复制代码
If you need to ensure the order of execution, you should use an array or nesting, for example:
Array method (recommended)
复制代码
Or nested way
复制代码
globalContext
Built-in global execution context
import TaroScript, { globalContext } from "taro-script";;复制代码
At this timeglobalContext. The value of value
is100
Customcontext
Example
import TaroScript from "taro-script"; const app = getApp();;复制代码
this When the value ofapp.value
is100
TaroScript
attributesrc
Type:string | string[]
Remote script to load
text
Type :string | string[]
The JavaScript script string that needs to be executed,text
has a higher priority thansrc
context
Type:object
Default value:globalContext = {}
Execution context, Default isglobalContext
numberDefault value:
10000milliseconds
()=> void
(err:Error)=> void
() => void
textis invalid when it exists
(err: Error) => void
textis invalid when it exists
React.ReactNode
boolean
true
key, and the caching period is the current user using the application Program life cycle.
React.ReactNode | ((context: T) => React.ReactNode)
functionThe first parameter is the context of script execution
Get the current execution context hookimport TaroScript, { useScriptContext } from "taro-script";evalScript(code: string, context?: {}); function Test() { const ctx = useScriptContext(); return ctx.a; // 100 }复制代码
Dynamicly execute the given string script and return The value of the last expressionimport { evalScript } from "taro-script"; const value = evalScript("100+200"); // 300复制代码
Others
syntax and supportsES5
NaN,Infinity,undefined,null,Object,Array,String,Boolean,Number,Date,RegExp,Error,URIError,TypeError,RangeError,SyntaxError,ReferenceError,Math,parseInt,parseFloat,isNaN,isFinite,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,escape,unescape,eval,Function,console, setTimeout, clearTimeout, setInterval, clearInterval,复制代码
Import a custom method or type example:
import TaroScript, { globalContext } from "taro-script"; globalContext.hello = function(){ console.log('hello taro-script') };复制代码
or custom context
import TaroScript from "taro-script"; const ctx = { hello(){ console.log('hello taro-script') } }If you want to learn more about programming, please stay tuned;复制代码
phptraining
The above is the detailed content of taro-script 0.4 released, learn about the js interpreter component based on Taro v3. For more information, please follow other related articles on the PHP Chinese website!