<компонент-которому-нужен-id-пользователя v-if="$store.getters.userId">
watch: {
'$store.getters.userId': {
immediate: true,
handler(userId) {
if (userId) {
...
}
},
},
},
array_filter($arr1, fn($n) => in_array($n, $arr2), ARRAY_FILTER_USE_KEY)
array_map(fn($n) => $arr1[$n], $arr2)
item-class
. Может быть функцией, принимающей объект с данными строки, и возвращающей классы:<v-data-table
:item-class="itemClasses"
...
methods: {
itemClasses: item => ({
red: !(item.id % 3),
}),
...
},
.red {
background: red;
}
top: 0;
для .pic
. const elements = document.querySelector('#list').children;
// или
const elements = document.querySelectorAll('#list li');
const getVal = el => +el.textContent.split(' - ').pop();
// или
const getVal = el => parseInt(el.innerText.replace(/.*\D/, ''));
// или
const getVal = el => el.innerHTML.match(/\d+$/) | 0;
const sum = Array.prototype.reduce.call(
elements,
(acc, n) => acc + getVal(n),
0
);
// или
let sum = 0;
for (const n of elements) {
sum += getVal(n);
}
// или
const sum = (function sum(i) {
return elements[i] ? getVal(elements[i]) + sum(i + 1) : 0;
})(0);
// или
const sum = eval(Array.from(elements, getVal).join('+')) ?? 0;
const digitsOnly = str => str.replace(/\D/g, '');
// или
const digitsOnly = str => (str.match(/\d/g) ?? []).join('');
// или
const digitsOnly = str => ''.concat(...str.matchAll(/\d/g));
// или
const digitsOnly = str => str.split(/\D/).join``;
// или
const digitsOnly = str =>
Array.prototype.reduce.call(
str,
(acc, n) => acc + ('0123456789'.includes(n) ? n : ''),
''
);
document.querySelectorAll('span').forEach(n => {
n.innerText = digitsOnly(n.innerText);
});
// или
for (const n of document.getElementsByTagName('span')) {
n.textContent = digitsOnly(n.textContent);
}
else
:if (state) {
this.x = 'c';
} else if (route) {
this.x = 'b';
} else if (prop) {
this.x = 'a';
}
this.x = [
[ state, 'c' ],
[ route, 'b' ],
[ prop, 'a' ],
].find(n => n[0])?.[1] ?? this.x;
this.x =
state ? 'c' :
route ? 'b' :
prop ? 'a' :
this.x;
switch (true) {
case !!state: this.x = 'c'; break;
case !!route: this.x = 'b'; break;
case !!prop: this.x = 'a'; break;
}
mounted () { ...
x
вычисляемым свойством. v-if="showModal"
на ref="modal"
, добавитьwatch: {
showModal(val) {
$(this.$refs.modal).modal(val ? 'show' : 'hide');
},
},
RegExp
в цепочке прототипов: x instanceof RegExp
.RegExp
конструктором значения: x?.constructor === RegExp
.const type = x => x == null ? x : x.constructor;
type() === undefined // true
type(null) === null // true
type(/./) === RegExp // true
type(666) === Number // true
type(type) === Function // true
const typename = x => x?.constructor.name ?? `${x}`;
typename() // 'undefined'
typename(null) // 'null'
typename(false) // 'Boolean'
typename(187) // 'Number'
typename('hello, world!!') // 'String'
typename(/./) // 'RegExp'
typename({}) // 'Object'
typename([]) // 'Array'
typename(document.body) // 'HTMLBodyElement'
typename(document.getElementsByClassName('xxx')) // 'HTMLCollection'
typename(new class XXX {}) // 'XXX'
typename(typename) // 'Function'
const newArr = arr.reduce((acc, n, i) => (acc.push(n + (acc[i - 1] ?? 0)), acc), []);
// или
const newArr = arr.reduce((acc, n) => (acc[0].push(acc[1] += n), acc), [ [], 0 ])[0];
// или
const newArr = arr.map((n, i, a) => eval(a.slice(0, i + 1).join('+')));
// или
const newArr = arr.map((n, i, a) => a.reduce((acc, m, j) => acc + m * (j <= i), 0));
arr.forEach((n, i, a) => a[i] += a[i - 1] ?? 0);
например на body
<button @click="onClick">
methods: {
onClick() {
document.body.classList.toggle('active');
},
},
<div :class="headerClass">
computed: {
headerClass() {
return в зависимости от this.$route;
},
},
methods: {
onKeyPress(e) {
if (/* нажато что надо */) {
this.$refs.searchInput.focus();
}
},
},
created() {
document.addEventListener('keypress', this.onKeyPress);
this.$on('hook:beforeDestroy', () => document.removeEventListener('keypress', this.onKeyPress));
},