简体   繁体   English

什么是mappings.ts文件,应如何在Tritium中进行设置?

[英]What is the mappings.ts file and how should it be set up in Tritium?

I'm using the Moovweb SDK and using Tritium. 我正在使用Moovweb SDK和Tritium。 I want my mobile site to behave like my desktop site. 我希望我的移动网站的行为类似于我的桌面网站。 I have different URLs pointing to my homepage. 我有指向我主页的不同URL。 Should I use regex? 我应该使用正则表达式吗? A common element? 一个共同的要素? And what's the best syntax for matching the path? 匹配路径的最佳语法是什么?

The mappings.ts file in the scripts directory is where particular pages are matched. 脚本目录中的mappings.ts文件是匹配特定页面的位置。 The file is imported in html.ts and allows us to say "when a certain page is matched, make the following transformations." 该文件被导入html.ts中,并允许我们说“当某个页面匹配时,进行以下转换”。

Most projects already have a mappings file generated. 大多数项目已经生成了映射文件。 A simple layout will be as so: 一个简单的布局将是这样的:

match($path) {
  with(/home/) {
    log("--> Importing pages/homes.ts in mappings.ts")
    @import pages/home.ts
  }
}

Every time you start working on a new page, you need to set up a new "map". 每次您开始在新页面上工作时,都需要设置一个新的“地图”。

First: Match with a unique path 第一:匹配唯一路径

The Tritium above matches the path for the homepage. 上面的Tritium与首页的路径匹配。 The path is the bit of a URL after the domain. 路径是域后的URL位。 For example, in www.example.com/search/item, "www.example.com" is the domain and "search/item" is the path. 例如,在www.example.com/search/item中,“ www.example.com”是域,而“ search / item”是路径。

The < >/home/< > is specifying the "home" part with regular expressions. < >/home/< >用正则表达式指定“ home”部分。 You could also use a plain string if necessary: 如果需要,您还可以使用纯字符串:

with("home") If Tritium matches the path with the matcher, it will import the home page. with(“ home”)如果Tritium将路径与匹配器匹配,它将导入主页。

It's probably true that the homepage of a site doesn't actually contain the word home. 网站的首页实际上并不包含“家”这个词,这很可能是事实。 Most homepages are the URL without any matcher. 大多数首页是没有任何匹配项的URL。 A better string matcher could be: 更好的字符串匹配器可以是:

match($path) {
  with ("/")
}

Or, using regex: 或者,使用正则表达式:

with(/index|^\/$/) {

As you can see, the < >with()< > function of the mappings file is where knowledge of Regex can really come in handy. 如您所见,映射文件的< >with()< >函数是真正可以使用Regex的地方。 Check out our short guide on regex. 查看我们关于正则表达式的简短指南。 Sometimes it will be simpler, such as < >(/search/)< >. 有时会更简单,例如< >(/search/)< >。

Remember to come up with the most unique aspect of the URL possible. 切记要想出URL中最独特的方面。 If two < >with()< > functions match the same URL, then the one that appears first in the mappings file will be used. 如果两个< >with()< >函数匹配相同的URL,则将使用映射文件中最先出现的那个。 If you cannot find a unique URL matcher for different page types, you may have to match via other means. 如果找不到用于不同页面类型的唯一URL匹配器,则可能必须通过其他方式进行匹配。

Why Use Regex? 为什么使用正则表达式?

It might seem easier to use a string rather than a regex matcher. 使用字符串而不是使用正则表达式匹配器似乎更容易。 However, regex provides a lot more flexibility over which URLs are matched. 但是,正则表达式在匹配URL方面提供了更大的灵活性。

For example, a site could use a string of numbers in its product page URLs. 例如,站点可以在其产品页面URL中使用数字字符串。 Using a normal string matcher would not be practical - you'd have to list out all the numbers possible for all the items on the site. 使用普通的字符串匹配器不切实际-您必须列出站点上所有项目可能的所有数字。 An easier way would be to use regex to say, "If there's a string of 5 digits, continue!" 一种更简单的方法是使用正则表达式说:“如果有5位数字的字符串,请继续!” (The code for matching 5 digits: < >/\\d{5}/< >.) (用于匹配5位数字的代码:< >/\\d{5}/< >。)

Second: Log the match 第二:记录比赛

When matching a particular path, you should also use < >log()< > statements so you know exactly what's getting imported. 匹配特定路径时,还应使用< >log()< >语句,以便确切了解要导入的内容。 The log statement will be printed in the command line window, so you can see if your regular expression accurately matches your path. log语句将打印在命令行窗口中,因此您可以查看正则表达式是否与您的路径精确匹配。

match($path) {
  with(/index|^\/$/) {
    log("--> importing pages/home.ts in mappings.ts")
  }
}

Third: Import the file 第三:导入文件

Finally, use the < >@import< > function to include the page-specific tritium file. 最后,使用< >@import< >函数来包含特定于页面的file文件。

match($path) {
  with(/index|^\/$/) {
    log("--> importing pages/home.ts in mappings.ts")
    @import pages/home.ts
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM