Ошибка подгрузки товаров в корзину
Реализована карточка товара на Vue, однако если внутри товара не найден класс product-type, появляется ошибка и скрипты не отрабатывают. Очень хочу понять, что я упустил
"use strict"
if (document.querySelector('.product-type')) {
let tableProductTypesToArray = (table) => {
let tableRows = table.querySelectorAll('tr');
let productName = document.getElementById('main-name').value;
let productTypes = [];
for (let i = 0; i < tableRows.length; i++) {
const last = tableRows[i].querySelectorAll('td').length-1;
productTypes.push(
{
name: productName + tableRows[i].querySelectorAll('td')[0].innerHTML,
price: tableRows[i].querySelectorAll('td')[last].innerHTML,
quantity: 0,
}
);
};
return productTypes;
};
let changeCells = (table) => {
let tableRows = table.querySelectorAll('tr');
for (let i = 0; i < tableRows.length; i++) {
const last = tableRows[i].querySelectorAll('td').length-1;
const cellPrice = tableRows[i].querySelectorAll('td')[last];
cellPrice.innerHTML === 'по запросу' ? '' : cellPrice.innerHTML += ' руб.';
let cellQuantity = document.createElement('td');
cellQuantity.setAttribute('item', i);
cellQuantity.innerHTML =
`
<div class="quantity">
<button @click="quantityMinus(${i})" class="quantity__btn">-</button>
<input @input="quantityInput(${i})" v-model="product.productTypes[${i}].quantity" class="quantity__number" type="text" name="" value="{{ product.productTypes[${i}].quantity }}">
<button @click="quantityPlus(${i})" class="quantity__btn">+</button>
<button @click="addToCart(${i})" class="quantity__btn">
<i class="fa fa-2x fa-cart-arrow-down"></i>
</button>
</div>
`;
tableRows[i].appendChild(cellQuantity);
};
};
const tableProductTypes = document.querySelector('.product-type').querySelector('table');
var productTypes = tableProductTypesToArray(tableProductTypes);
changeCells(tableProductTypes);
} else {
var productTypes = [];
}
let cart = new Vue({
el: '.page-content',
// props: ['productTypes'],
data: {
product: {
name: '',
productTypes: productTypes,
},
cart: [],
isCartVisible: false,
cartNumber: 0,
},
mounted() {
if (localStorage.cart) {
this.cart = JSON.parse(localStorage.getItem('cart'));
}
},
methods: {
quantityPlus(item) {
this.product.productTypes[item].quantity++;
},
quantityMinus(item) {
if(this.product.productTypes[item].quantity >= 1) {
this.product.productTypes[item].quantity--;
};
},
quantityInput(item) {
if(this.product.productTypes[item].quantity < 0 || this.product.productTypes[item].quantity === '') {
this.product.productTypes[item].quantity = 0;
};
},
localStorage() {
return JSON.parse(localStorage.getItem('cart'))
},
addToCart(item) {
if (this.product.productTypes[item].quantity >= 1) {
if( this.cart.indexOf(this.product.productTypes[item]) === -1 ) {
this.cart.push(this.product.productTypes[item]);
}
};
this.cart.concat(this.localStorage());
},
deleteCartItem(item) {
this.cart.splice(item, 1);
},
summCartItem(price, quantity){
var linePrice = price*quantity;
if (isNaN(linePrice)) {
return "по запросу";
} else {
return linePrice.toFixed(2);
}
},
},
computed: {
getCart() {
this.cart = JSON.parse(localStorage.getItem('cart'));
},
isCartEmpty() {
if (this.cart.length <= 0) {
return true
} else {
return false
}
},
},
watch: {
cart(newCart) {
newCart = JSON.stringify(newCart);
localStorage.setItem('cart', newCart);
}
},
});