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 v3development, supports multi-terminal small programs to dynamically load remote JavaScript scripts and execute them,supports ES5 syntax
useScriptContextGet the current execution contexttextAttribute, you can directly pass in the js stringsrcSupports 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-scriptwill only be executed once, that is, aftercomponentDidMount. Subsequent changes to the properties will be invalid. Example
function App({ url }) { // 只会在第一次创建后加载并执行,后续组件的更新会忽略所有属性变动 return ; }复制代码
Note 2: Multipletaro-scriptwill 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
复制代码
globalContextBuilt-in global execution context
import TaroScript, { globalContext } from "taro-script"; ;复制代码
At this timeglobalContext. The value of valueis100
CustomcontextExample
import TaroScript from "taro-script"; const app = getApp();;复制代码
this When the value ofapp.valueis100
TaroScriptattributesrcType:string | string[]
Remote script to load
textType :string | string[]
The JavaScript script string that needs to be executed,texthas a higher priority thansrc
contextType: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"; ; function Test() { const ctx = useScriptContext(); return ctx.a; // 100 }复制代码
evalScript(code: string, context?: {})
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!