我正在尝试使用v-forVue.js中的方法在每个逗号上分割字符串。
输入: tags: 'test1, test2, test3'
方法: <span v-for="tag in tags" v-text="tag"/>
输出: t e s t 1 , t e s t 2 , t e s t 3
所需输出: test1 test2 test3
如何正确分割逗号分隔的文字?除非v-for为了调用,我处于循环中,否则我将无法访问“标签”输入object.tags。
不知道为什么这不起作用,尽管在我的代码中几乎是一样的东西:https : //jsfiddle.net/9o3o11jc/1/
如何优化大量的开关案例?还有其他方法可以做我正在尝试做的事情吗?
我有一个时间滑块,这个滑块currentTime用当前滑块所在的值(1-24)更新变量并调用updateTime()方法.在这种方法中,我的切换情况为1 - 24(本例中仅为3).我可以用更简单的方式做到这一点,而不是制作24个开关盒吗?
private void updateTime() {
switch (currentTime) {
case 1:
hourlyData = weatherAPI.HourlyReport(1);
setHourlyData();
break;
case 2:
hourlyData = weatherAPI.HourlyReport(2);
setHourlyData();
break;
...
case 24:
hourlyData = weatherAPI.HourlyReport(24);
setHourlyData();
break;
default:
System.out.println("Oops");
break;
}
}
Run Code Online (Sandbox Code Playgroud)
-
public Map HourlyReport(int hour) {
Hourly hourly = new Hourly(fio);
//In case there is no hourly data available
if (hourly.hours() < 0) {
System.out.println("No hourly data.");
} else {
hourlyData.put("Temp", hourly.getHour(hour).temperature()); // Temperature
hourlyData.put("TempFeel", hourly.getHour(hour).apparentTemperature()); // Feel …Run Code Online (Sandbox Code Playgroud) 我试图调用GetLikes(item.id)方法,它是在forEach和我的内axios.get功能。我得到一个错误提示TypeError: Cannot read property 'GetLikes' of undefined。
如果我评论该方法,我可以看到我能够获取所有项目及其ID,但是当我取消对该方法的注释时,它将不再起作用。
axios
.get("/api/endpoint")
.then(response => {
this.data = response.data;
this.data.forEach(function(item) {
console.log("found: ", item)
console.log("found id: ", item.id)
this.GetLikes(item.id);
});
})
Run Code Online (Sandbox Code Playgroud)
上面的代码输出: 似乎由于某种原因它无法获得ID 1,尽管没有下面的方法,相同的代码也获得ID 1。
found: {…}
found id: 2
TypeError: Cannot read property 'GetLikes' of undefined
Run Code Online (Sandbox Code Playgroud)
注释掉this.GetLikes(item.id)的输出:
found: {…}
found id: 2
found: {…}
found id: 1
Run Code Online (Sandbox Code Playgroud)
^以上显然可以获取所有项目,因此,如果我尝试对这些项目调用方法,为什么会得到未定义的信息?
下面的代码有效(获得正确的赞)。我在用户按下“赞”按钮时使用它,但是我首先还需要获得所有赞,这就是我上面想要做的。
Like(id) {
axios
.post("/like/" + id)
.then(response => {
this.GetLikes(id);
})
}
Run Code Online (Sandbox Code Playgroud)
我在这里想念什么?