Vuejs
Vuejs
After the extraction is completed, go to the "backend" folder by typing the following command in the
terminal:
cd backend
Then type the following command in the terminal to run the project:
php spark serve
<?php
namespace App\Controllers;
use CodeIgniter\RESTful\ResourceController;
use CodeIgniter\API\ResponseTrait;
use App\Models\ProductModel;
<!—start here →
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With,
Content-Type, Accept, Access-Control-Request-Method");
install nodejs
verify if the nodejs is installed on your machine using this command
node -v
npm -v
Open PowerShell as Administrator: To change the Execution Policy, you'll need to open PowerShell with
administrative privileges. To do this, search for "PowerShell" in the Windows search bar, right-click on
"Windows PowerShell," and select "Run as administrator."
Check Current Execution Policy: You can check the current Execution Policy by running the following
command in the PowerShell window:
Get-ExecutionPolicy
Change Execution Policy: To change the Execution Policy to allow scripts to run, use the following
command:
Set-ExecutionPolicy RemoteSigned
This command allows scripts that you've written locally to run without a digital signature. You will be
prompted to confirm the change. Type "Y" and press Enter to confirm.
Set-ExecutionPolicy Unrestricted
Run Vue CLI: Once you've changed the Execution Policy, you should be able to run Vue CLI commands
without encountering the error. Try running your Vue CLI command again, for example:
vue -V
Select 3.x
The “backend” folder is the project we created earlier using CodeIgniter 4, while the “frontend” is the project we created using the Vue CLI.
Then, go to the “frontend” folder by typing the following command in the terminal:
cd frontend
Next, install the dependencies we need by typing the following command in the terminal:
npm install axios bootstrap@5.3.2
<style>
@import '~bootstrap/dist/css/bootstrap.css'
</style>
After that, open the "main.js" file located in the "frontend/src" folder, then change it to the following:
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
<script>
import axios from "axios";
export default {
name: "Product",
data() {
return {
products: [],
};
},
created() {
this.getProducts();
},
methods: {
async getProducts() {
try {
const response = await axios.get("product");
this.products = response.data;
} catch (error) {
console.log(error);
}
},
async deleteProduct(id) {
try {
await axios.delete(`product/${id}`);
this.getProducts();
} catch (error) {
console.log(error);
}
},
},
};
</script>
<style></style>
Then create another file named "ProductList.vue" in the "frontend/src/views" folder, then type the following code:
<template>
<Product />
</template>
<script>
import Product from "@/components/Product.vue";
export default {
name: "ProductList",
components: {
Product,
},
};
</script>
<style></style>
After that, open the “index.js” file located in the “frontend/src/router” folder, then change it to be as follows:
import { createRouter, createWebHistory } from 'vue-router'
import ProductList from '../views/ProductList.vue'
const routes = [
{
path: '/',
name: 'ProductList',
component: ProductList
}
]
<script>
import axios from "axios";
export default {
name: "AddForm",
data() {
return {
title: "",
price: "",
};
},
methods: {
async saveProduct() {
try {
await axios.post("product", {
title: this.title,
price: this.price,
});
(this.title = ""), (this.price = ""), this.$router.push("/");
} catch (error) {
console.log(error);
}
},
},
};
</script>
<style></style>
Then create another file named "AddProduct.vue" in the "frontend/src/views" folder, then type the following code:
<template>
<AddForm />
</template>
<script>
import AddForm from "@/components/AddForm.vue";
export default {
name: "AddProduct",
components: {
AddForm,
},
};
</script>
<style></style>
After that, open the “index.js” file located in the “frontend/src/router” folder, then change it to be as follows:
import { createRouter, createWebHistory } from 'vue-router'
import ProductList from '../views/ProductList.vue'
import AddProduct from '../views/AddProduct.vue'
const routes = [
{
path: '/',
name: 'ProductList',
component: ProductList
},
{
path: '/add',
name: 'AddProduct',
component: AddProduct
}
]
<form @submit.prevent="updateProduct">
<div class="field">
<label class="label">Title</label>
<div class="control">
<input
type="text"
v-model="title"
class="input"
placeholder="Title"
/>
</div>
</div>
<div class="field">
<label class="label">Price</label>
<div class="control">
<input
type="text"
v-model="price"
class="input"
placeholder="Price"
/>
</div>
</div>
<div class="field">
<div class="control">
<button class="button is-primary">Update</button>
</div>
</div>
</form>
</div>
</template >
<script>
import axios from "axios";
export default {
name: "EditForm",
data() {
return {
title: "",
price: "",
};
},
created() {
this.getProductById();
},
methods: {
async getProductById() {
try {
const response = await axios.get(`product/${this.$route.params.id}`);
(this.title = response.data.title), (this.price = response.data.price);
} catch (error) {
console.log(error);
}
},
async updateProduct() {
try {
await axios.put(`product/${this.$route.params.id}`, {
title: this.title,
price: this.price,
});
(this.title = ""), (this.price = ""), this.$router.push("/");
} catch (error) {
console.log(error);
}
},
},
};
</script>
<style>
</style>
<template>
<EditForm />
</template>
<script>
import EditForm from "@/components/EditForm.vue";
export default {
name: "EditProduct",
components: {
EditForm,
},
};
</script>
<style>
</style>
After that, open the “index.js” file located in the “frontend/src/router” folder, then change it to be as follows:
import { createRouter, createWebHistory } from 'vue-router'
import ProductList from '../views/ProductList.vue'
import AddProduct from '../views/AddProduct.vue'
import EditProduct from '../views/EditProduct.vue'
const routes = [
{
path: '/',
name: 'ProductList',
component: ProductList
},
{
path: '/add',
name: 'AddProduct',
component: AddProduct
},
{
path: '/edit/:id',
name: 'EditProduct',
component: EditProduct
},
]