字符串模板

模板文本是ES2015/ES6中引入的一个的JavaScript功能。

它们允许您使用内插值构建多行字符串,并使它很容易处理嵌入在字符串中的单引号和双引号。

此示例显示如何使用wijmo的globalize类添加格式以及模板文本的国际化。

import 'bootstrap.css'; import '@grapecity/wijmo.styles/wijmo.css'; import { i18n } from './i18n'; document.readyState === 'complete' ? init() : window.onload = init; function init() { // show first result document.getElementById('btn1').addEventListener('click', () => { document.getElementById('result1').textContent = i18n `Today is‌ ${new Date()}:d, and PI is‌ ${Math.PI}:n4.`; }); // show second result document.getElementById('btn2').addEventListener('click', () => { document.getElementById('result2').textContent = i18n `Today is‌ ${new Date()}:d, and it's ${new Date()}:T.`; }); } <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Globalization/Template Literals</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app'); </script> </head> <body> <div class="container-fluid"> <p> Click the button below to evaluate this template literal using a <b>i18n</b> tag function that formats numbers and dates using Wijmo's <b>Globalize</b> class: </p> <pre> i18n`Today is $‌&zwnj;{new Date()}:d, and PI is $‌&zwnj;{Math.PI}:n4.`</pre> <button id="btn1" class="btn btn-default"> Show Result </button> <pre id="result1"></pre> <p> This example shows how you can format times: </p> <pre> i18n`Today is‌ $&zwnj;{new Date()}:d, and it's $&zwnj;{new Date()}:T.`</pre> <button id="btn2" class="btn btn-default"> Show Result </button> <pre id="result2"></pre> </div> </body> </html> import 'bootstrap.css'; import { Component, enableProdMode, NgModule } from '@angular/core'; import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; import { BrowserModule } from '@angular/platform-browser'; import { i18n } from './i18n'; @Component({ selector: 'app-component', templateUrl: 'src/app.component.html' }) export class AppComponent { result1 = ''; format1() { this.result1 = i18n`Today is‌ ${new Date()}:d, and PI is‌ ${Math.PI}:n4.`; } result2 = ''; format2() { this.result2 = i18n`Today is‌ ${new Date()}:d, and it's ${new Date()}:T.`; } } @NgModule({ imports: [BrowserModule], declarations: [AppComponent], bootstrap: [AppComponent] }) export class AppModule { } enableProdMode(); // Bootstrap application with hash style navigation and global services. platformBrowserDynamic().bootstrapModule(AppModule); <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Globalization/Template Literals</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Polyfills --> <script src="node_modules/core-js/client/shim.min.js"></script> <script src="node_modules/zone.js/dist/zone.min.js"></script> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.js"></script> <script src="systemjs.config.js"></script> <script> // workaround to load 'rxjs/operators' from the rxjs bundle System.import('rxjs').then(function (m) { System.set(SystemJS.resolveSync('rxjs/operators'), System.newModule(m.operators)); System.import('./src/app.component'); }); </script> </head> <body> <app-component></app-component> </body> </html> <div class="container-fluid"> <p> Click the button below to evaluate this template literal using a <b>i18n</b> tag function that formats numbers and dates using Wijmo's <b>Globalize</b> class: </p> <pre> i18n`Today is $‌{{'{'}}new Date()}:d, and PI is $‌{{'{'}}Math.PI}:n4.`</pre> <button id="btn1" class="btn btn-default" (click)="format1()"> Show Result </button> <pre id="result1">{{result1}}</pre> <p> This example shows how you can format times: </p> <pre> i18n`Today is‌ ${{'{'}}new Date()}:d, and it's ${{'{'}}new Date()}:T.`</pre> <button id="btn2" class="btn btn-default" (click)="format2()"> Show Result </button> <pre id="result2">{{result2}}</pre> </div> <template> <div class="container-fluid"> <p> Click the button below to evaluate this template literal using a <b>i18n</b> tag function that formats numbers and dates using Wijmo's <b>Globalize</b> class: </p> <pre> i18n\`Today is $‌\{new Date()}:d, and PI is $‌\{Math.PI}:n4.\`</pre> <button id="btn1" class="btn btn-default" @click="format1"> Show Result </button> <pre id="result1">{{result1}}</pre> <p> This example shows how you can format times: </p> <pre> i18n\`Today is‌ $\{new Date()}:d, and it's $\{new Date()}:T.\`</pre> <button id="btn2" class="btn btn-default" @click="format2"> Show Result </button> <pre id="result2">{{result2}}</pre> </div> </template> <script> import "bootstrap.css"; import Vue from "vue"; import { i18n } from './i18n'; let App = Vue.extend({ name: "app", data: function() { return { result1: '', result2: '' }; }, methods: { format1: function() { this.result1 = i18n`Today is ${new Date()}:d, and PI is ${Math.PI}:n4.`; }, format2: function() { this.result2 = i18n`Today is‌ ${new Date()}:d, and it's ${new Date()}:T.`; } } }); new Vue({ render: h => h(App) }).$mount("#app"); </script> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <title>GrapeCity Globalization/Template Literals</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- SystemJS --> <script src="node_modules/systemjs/dist/system.src.js"></script> <script src="systemjs.config.js"></script> <script> System.import('./src/app.vue'); </script> </head> <body> <div id="app"> </div> </body> </html>