在 Vue 3(组合 API)Pinia 存储上初始化状态时,哪种模式更“正确”或更惯用?
选项1:
state: () => ({
user: {},
}),
Run Code Online (Sandbox Code Playgroud)
选项2:
state: () => {
return {
user: {},
};
},
Run Code Online (Sandbox Code Playgroud)
选项3:也许还有别的东西?
我需要一个简单的 JavaScript 计数器,它在页面加载时从 0 开始计数,并计数到 HTML 中定义的数字。
这是 jQuery 版本……我如何用纯 JavaScript 做同样的事情?
$('.count').each(function () {
$(this).prop('Counter',0).animate({
Counter: $(this).text()
}, {
duration: 4000,
easing: 'swing',
step: function (now) {
$(this).text(Math.ceil(now));
}
});
});Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<span class="count">200</span>%Run Code Online (Sandbox Code Playgroud)
以前在 Firebase 中,您可以添加如下文档:
const myNewDoc = await db.collection('some-collection-name').add({ //Document details here... });
Run Code Online (Sandbox Code Playgroud)
随着 Firebase 9 的推出,这种情况不再有效。
相反,.add我认为我应该使用导入的.addDoc方法。
但看来我无法链接.addDoc到.collection.
如果我尝试做这样的事情:
const myNewDoc = await db.collection('some-collection-name').addDoc({ //Document details here... });
Run Code Online (Sandbox Code Playgroud)
TypeScript 抛出此错误:
Property 'addDoc' does not exist on type 'CollectionReference<DocumentData>'.ts(2339)
Run Code Online (Sandbox Code Playgroud)
我可以创建更详细的内容,如下所示:
const someCol = collection(db, "some-collection-name");
const newDoc = await addDoc(someCol, {
//Document details here...
});
Run Code Online (Sandbox Code Playgroud)
但我宁愿像以前一样“链接”它。
那可能吗?该怎么做呢?
我什至应该使用吗.addDoc?或者是其他东西?
如何在Quasar中实现类似的功能,而不需要重新定义每个组件中的变量:
<template>
<div>Welcome to {{ APP_NAME }}.</div>
</template>
Run Code Online (Sandbox Code Playgroud)
我的应用程序是使用 Quasar CLI 设置的,它在设置过程中要求输入应用程序名称,因此我想它会作为全局变量或我可以访问的内容存储在某个位置。
如果做不到这一点,也许 Vue 3 有办法做到这一点。
我正在尝试使用 prop 来控制子组件对父组件的可见性。
我的子组件有一个visible道具。为了避免改变visible道具,我将其分配给本地isVisible引用,并使用它来有条件地显示和隐藏表单。
但是,当我尝试使用按钮隐藏表单时,没有@click="isVisible = false"任何反应,并且出现控制台错误,提示Set operation on key "visible" failed: target is readonly.
为什么错误消息引用visibleprop 也令人困惑,因为我使用的是局部isVisible变量。
这是ChildForm.vue(子组件):
<template>
<q-form v-if="isVisible">
<q-input v-model="modelValue" />
<q-btn label="Hide The Form" @click="isVisible = false" />
</q-form>
</template>
<script setup lang="ts">
import { ref, toRef } from 'vue';
const props = defineProps({
visible: {
type: Boolean,
required: true,
default: false,
},
});
const isVisible = toRef(props, 'visible');
const …Run Code Online (Sandbox Code Playgroud) vue.js quasar-framework vuejs3 vue-composition-api vue-script-setup
我有一个看起来像这样的数组:
const subscriptions = [
{
"price": "20",
"product": "apple",
"quantity": 1,
},
{
"price": "10",
"product": "orange",
"quantity": 1,
},
{
"price": "10",
"product": "orange",
"quantity": 1,
},
{
"price": "10",
"product": "orange",
"quantity": 1,
},
]
Run Code Online (Sandbox Code Playgroud)
我想取出所有具有apple、banana或 的“乘积”的数组元素pear。
filter()所以我这样使用:
const currentPlans = subscriptions.filter(
(subscription) =>
subscription.product ===
('apple' || 'banana' || 'pear')
);
Run Code Online (Sandbox Code Playgroud)
由于数组只有一个实例,apple因此应该currentPlans包含该实例。
但currentPlans返回一个空数组。
我究竟做错了什么?
我需要创建一个将生成错误的首次输入延迟 (FID) 值的网页。
如果您不知道,FID 是 Google Web Core Vitals 的一部分。
我想模拟错误的 FID,因为我正在测试一个网站扫描工具,该工具应该标记错误的 FID 值。因此,我想在网页上模拟一个错误值以确保它有效。
需要明确的是 - 我并不是想修复我的第一次输入延迟。我想创建一个故意给出错误的“首次输入延迟”值的网页。
但我不知道该怎么做。
我有一个 HTML 页面,带有<button id="button">Click Me</button>. 在<head>我添加了这个脚本:
<script type="module">
// Get the First Input Delay (FID) Score
import {onFID} from 'https://unpkg.com/web-vitals@3/dist/web-vitals.attribution.js?module';
// Get the button element
const button = document.getElementById('button');
// Add a click event listener to the button
button.addEventListener('click', async () => {
// Make a delay
await new Promise((resolve) => setTimeout(resolve, 5000));
// Print the FID score …Run Code Online (Sandbox Code Playgroud) 我正在使用它来显示 WooCommerce 产品标题:
<?php print $_product->get_title(); ?>
但它只显示父产品的标题。如何显示变体的标题?
我正在学习c ++和测试问题我正在使用带有条件的for循环,prices.size()-1因此向量不会超出范围.
std::vector<int> prices {7,1,5,3,6,4};
int maxProfit(vector<int>& prices) {
int total {0};
for (size_t i = 0; i < prices.size()-1; i++) {
if (prices.at(i+1) > prices.at(i)) {
total += prices.at(i+1) - prices.at(i);
}
}
return total;
}
Run Code Online (Sandbox Code Playgroud)
但它抛出了这个我无法破译的运行时错误:
terminate called after throwing an instance of 'std::out_of_range' what(): vector::_M_range_check: __n (which is 1) >= this->size() (which is 0)
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚问题,有人可以告诉我我做错了什么.
当时间达到 5 分钟到一小时的顶部时,我想将用户重定向到另一个页面。
在 24 小时内,这意味着我希望重定向以这样的间隔运行......
到目前为止,我所能弄清楚的就是像这样的 JS 重定向的“倒计时”风格,但不是这样,我需要一些基于时间(到一小时前 5 分钟)而不是倒计时的东西。
setTimeout("location.href = 'https://www.google.com';",1000);
我也试过这个,但什么也没发生。
var mins = new Date().getMinutes();
if (mins == 55) {
window.location.href = "https://www.google.com";
}
Run Code Online (Sandbox Code Playgroud)
JavaScript(或 jQuery)可以做到这一点吗?
javascript ×6
vue.js ×3
vuejs3 ×2
c++ ×1
firebase ×1
jquery ×1
pinia ×1
seo ×1
typescript ×1
web-vitals ×1
woocommerce ×1