变量文件:
export class VariableSettings {
public static string_value: string = 'vikas/${id}/data';
}
Run Code Online (Sandbox Code Playgroud)
其他文件.
import {VariableSettings} from './variables';
getData(id:string ){
console.log(`${VariableSettings.string_value}`, `${id}`);
// it prints vikas/${id}/data abcd11123
}`
Run Code Online (Sandbox Code Playgroud)
现在我希望结果像"vikas/abcd11123/data".那么如何在该字符串中注入id.
任何有关相同的建议将不胜感激.
谢谢
javascript interpolation typescript template-strings angular
我将尝试一步一步地学习Express库和Node.js.首先我要看的是Node reqiure(moduleName)函数的细节.
我看了一下这个文档,并在示例文档中找到了一些奇怪的代码:
const circle = require('./circle.js');
console.log( `The area of a circle of radius 4 is ${circle.area(4)}`);
Run Code Online (Sandbox Code Playgroud)
更具体地说是${circle.area(4)}位.
根据我的理解,$在JavaScript中就像任何其他变量一样.当我们在客户端Web开发中使用它时,它被用作文档功能的委托(我认为).使用节点时分配的是什么?
最重要的是,这种语法意味着什么? ${circle.area(4)}
如果$只是对某个函数的引用someFunction(),那么它不等同于此someFunction(){cirle.area(4)}.我没有看到这可能是有效的语法.
另外,他们为什么不直接直接调用circle.area()函数呢?
javascript node.js ecmascript-6 template-strings template-literals
我有一个包含我想要替换的子串的字符串,例如
text = "Dear NAME, it was nice to meet you on DATE. Hope to talk with you and SPOUSE again soon!"
Run Code Online (Sandbox Code Playgroud)
我有一个格式的csv(第一行是标题)
NAME, DATE, SPOUSE
John, October 1, Jane
Jane, September 30, John
...
Run Code Online (Sandbox Code Playgroud)
我正在尝试循环遍历csv文件中的每一行,text使用与原始子字符串匹配的标题行的列中的csv元素替换子字符串.我有一个名单matchedfields,其中包含在csv标题行中找到的所有字段text(如果csv 中有一些列我不需要使用).我的下一步是遍历每个csv行并将匹配的字段替换为该csv列中的元素.要做到这一点,我正在使用
with open('recipients.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
for match in matchedfields:
print inputtext.replace(match, row[match])
Run Code Online (Sandbox Code Playgroud)
我的问题是,它只用textcsv中的相应元素替换第一个匹配的子字符串.有没有办法同时进行多次替换,所以我最终得到了
"Dear John, it was nice to meet you on October 1. Hope to talk with you …Run Code Online (Sandbox Code Playgroud) 我可以创建一个将键值作为模板字符串传递的对象吗?
const key = 'my-key', value = 'my-value'
const obj = {
`${key}`: value
}
Run Code Online (Sandbox Code Playgroud)
有替代方法吗?
我正在尝试使用 npm 包发出 graphql 请求graphql-request。我正在发现模板文字的使用。
async getCandidate(userId: number) {
const query = gql`
query($userId: ID){
candidate(id: $userId){
id _id source phone
}
}
`
const variables = {userId: "/api/candidates/" + userId}
return await request(GRAPHQL_URL, query, variables)
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试使用该usedId变量,但出现错误:
Variable "$userId" of type "ID" used in position expecting type "ID!".: {"response":{"errors":[{"message":"Variable \"$userId\" of type \"ID\" used in position expecting type \"ID!\".","extensions":{"category":"graphql"},"locations":[{"line":2,"column":9},{"line":3,"column":19}]}],"status":200},"request":{"query":"\n\t\tquery($userId: ID){\n\t\t candidate(id: $userId){\n\t\t id _id source phone\n\t\t }\n\t\t}\n\t\t","variables":{"userId":"/api/candidates/1"}
Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的开发中使用ES6/ES2015功能,并尝试使用模板字符串代替串联.
我有一个名为的文件meteor.jsx,其中包含以下代码.
getLocation(lat,lon){
return Meteor.http.call('GET','http://maps.googleapis.com/maps/api/geocode/json?latlng=${ lat },${ lon }&sensor=true&callback=zipmap')
}
Run Code Online (Sandbox Code Playgroud)
如果我用实际坐标称呼它
Meteor.call('getLocation','37.3175','-122.0419',function(e,r){}
Run Code Online (Sandbox Code Playgroud)
它不会转换lat或转换lon为字符串,只是在返回字符串中打印'$ {lat}'和'$ {lon}'.我究竟做错了什么?
我正在使用node.js创建一个项目.我正在使用yo-yo库来创建html模板.
我想从tweet用户profile_banner_url添加一个封面.
像这样:
const yo = require('yo-yo')
module.exports = function (tweet) {
return yo`
<div class='cover'>
<img src='${tweet.user.profile_banner_url}' />
</div>
`
}
Run Code Online (Sandbox Code Playgroud)
但是,有时推文不会返回任何profile_banner_url,这会在浏览器中出错.
我试图从public目录中添加不同的图像:
<img src='${tweet.user.profile_banner_url} || otherimg.jpg' />
Run Code Online (Sandbox Code Playgroud)
但它没有用.
or在模板字符串中使用条件的最佳方法是什么?
我在tbody中显示表格数据时遇到问题,在我的javascript中我得到了结果对象,并且我在javascript文件中使用了模板文字,这是我的代码:
for (i = 0; i < size; i++)
{
serialNo++;
html += `<tr>
<td>${serialNo}</td>
<td>${result[i].first_name}</td>
<td>${result[i].last_name}</td>
<td>${result[i].email}</td>
<td>${result[i].mobile}</td>
(${(result[i].status == 1)} ? '<td><div class="switchery-demo">
<input type="checkbox" checked id="demo${result[i].contact_id}"
onclick="status_item(this,'${result[i].contact_id}')"
data-plugin="switchery" data-size="small" data-color="#1bb99a"/>
</div></td>' : '<td><div class="switchery-demo"><input
type="checkbox" checked data-plugin="switchery"
id="demo${result[i].contact_id}"
onclick="status_item(this,'${result[i].contact_id}')" data-
size="small" data-color="#1bb99a"/></div></td>')
</tr>`;
}
Run Code Online (Sandbox Code Playgroud)
在result [i] .status中,我正在使用三元运算符,我正在检查status == 1,然后显示另一个,然后显示第二个,但它创建的是两个而不是一个,我不知道我要去哪里,请帮忙。
我正在尝试使用 nodemailer 从我的服务器发送电子邮件。不幸的是,由于这个错误,我一直无法对其进行测试:
D:\Full Stack\Node\NodeLoginJWT\functions\password.js:58
'This token is valid only within two minutes.'
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: Unexpected string
at createScript (vm.js:56:10)
at Object.runInThisContext (vm.js:97:10)
at Module._compile (module.js:542:28)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\Full Stack\Node\NodeLoginJWT\routes.js:9:18)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
[nodemon] app crashed - waiting for file changes before starting...
Run Code Online (Sandbox Code Playgroud)
这是导致错误的代码块:
const transporter = …Run Code Online (Sandbox Code Playgroud) 模板字符串应可在任何终端上使用,例如Visual Studio代码终端或Windows终端。但事实并非如此。我做了这段代码Visual Studio代码。这是我的代码
var name = 'Andrew';
console.log('Hello ${name}');Run Code Online (Sandbox Code Playgroud)
输出是
Hello ${name}
Run Code Online (Sandbox Code Playgroud)
Please specify changes in my code needed and explain why it doesn't work currently.
template-strings ×10
javascript ×6
ecmascript-6 ×4
node.js ×2
typescript ×2
angular ×1
graphql ×1
html ×1
meteor ×1
nodemailer ×1
python ×1
replace ×1
substring ×1