computed和watch的区别:
- computed是计算属性,通过属性计算得来的属性,而watch是监听一个值的变化,然后执行对应的回调1。
- computed中的函数直接调用,不用加(),而watch中的函数不需要调用1。
- computed是依赖data中的属性,data中属性发生改变的时候,当前函数才会执行,而watch不支持缓存1。
- computed属性的结果会被缓存,除非依赖的响应式属性变化才会重新计算,而watch一个对象,键是需要观察的表达式,值是对应回调函数。
computed和watch的运用场景:
- computed适合用在多个数据相互影响,需要处理复杂逻辑或多个属性影响一个属性的变化时使用,例如:购物车商品结算等。
- watch适合用在数据影响多个数据,需要在数据变化时执行异步操作或者开销较大的操作时使用,例如:搜索数据等。
在本教程中,您将学习如何使用switch-case语句根据Golang中的不同条件执行不同的操作。
Golang 还支持类似于其他语言(如Php或Java)中的 switch 语句。switch 语句是一种替代方法,用于根据变量的状态将冗长的 if else 比较表达为更具可读性的代码。
switch语句
switch语句用于选择要执行的多个代码块之一。
请考虑以下示例,该示例显示特定日期的不同消息。
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now()
switch today.Day() {
case 5:
fmt.Println("Today is 5th. Clean your house.")
case 10:
fmt.Println("Today is 10th. Buy some wine.")
case 15:
fmt.Println("Today is 15th. Visit a doctor.")
case 25:
fmt.Println("Today is 25th. Buy some food.")
case 31:
fmt.Println("Party tonight.")
default:
fmt.Println("No information available for that day.")
}
}
如果未找到匹配项,则使用default语句。
case拥有多个条件
case带有多个条件,用于为许多类似情况选择公共代码块。
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now()
var t int = today.Day()
switch t {
case 5, 10, 15:
fmt.Println("Clean your house.")
case 25, 26, 27:
fmt.Println("Buy some food.")
case 31:
fmt.Println("Party tonight.")
default:
fmt.Println("No information available for that day.")
}
}
fallthrough关键字
fallthrough关键字,用于强制执行流通过连续的事例块。
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now()
switch today.Day() {
case 5:
fmt.Println("Clean your house.")
fallthrough
case 10:
fmt.Println("Buy some wine.")
fallthrough
case 15:
fmt.Println("Visit a doctor.")
fallthrough
case 25:
fmt.Println("Buy some food.")
fallthrough
case 31:
fmt.Println("Party tonight.")
default:
fmt.Println("No information available for that day.")
}
}
下面是每月 10 日的输出。
Buy some wine.
Visit a doctor.
Buy some food.
Party tonight.
有条件case
case语句还可以与条件运算符一起使用。
package main
import (
"fmt"
"time"
)
func main() {
today := time.Now()
switch {
case today.Day() < 5:
fmt.Println("Clean your house.")
case today.Day() <= 10:
fmt.Println("Buy some wine.")
case today.Day() > 15:
fmt.Println("Visit a doctor.")
case today.Day() == 25:
fmt.Println("Buy some food.")
default:
fmt.Println("No information available for that day.")
}
}
switch初始值设定项语句
switch关键字可以紧跟一个简单的初始化语句,其中可以声明和初始化switch代码块的本地变量。
package main
import (
"fmt"
"time"
)
func main() {
switch today := time.Now(); {
case today.Day() < 5:
fmt.Println("Clean your house.")
case today.Day() <= 10:
fmt.Println("Buy some wine.")
case today.Day() > 15:
fmt.Println("Visit a doctor.")
case today.Day() == 25:
fmt.Println("Buy some food.")
default:
fmt.Println("No information available for that day.")
}
}
要使用JavaScript的fetch API上传文件,您需要使用FormData对象和fetch函数。
演示如何使用fetch API上传文件:
html<form id="fileUploadForm">
<input type="file" id="fileInput">
<button type="submit">上传</button>
</form>
<script>
document.getElementById("fileUploadForm").addEventListener("submit", function(event) {
event.preventDefault(); // 阻止表单默认提交行为
var fileInput = document.getElementById("fileInput");
var file = fileInput.files[0];
var formData = new FormData();
formData.append("file", file);
fetch("/upload", { // 替换为您自己的上传接口地址
method: "POST",
body: formData
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error(error));
});
</script>
我们首先获取文件输入元素和表单元素,然后创建一个FormData对象并将文件添加到其中。
接下来,我们使用fetch函数将表单数据发送到服务器。请注意,我们使用了POST方法并将表单数据作为请求体发送。
最后,我们使用then函数处理服务器响应并将结果输出到控制台。