Astro Cheatsheet
Traduzione:
__HTML_TAG_65_Astro - Il Web Framework per i siti web di Content-Driven__HTML_TAG_66_
Astro è un moderno generatore di sito statico che offre prestazioni veloci con una moderna esperienza di sviluppo. Costruire siti di contenuti come blog, marketing e e-commerce con i vostri componenti UI preferiti e distribuire ovunque.
Traduzione:
__HTML_TAG_70_
📋 Copia Tutti i comandi_HTML_TAG_72__
__HTML_TAG_73_📄 Generare PDF
Tavola dei contenuti
Installazione
Prerequisiti
Traduzione:
Create Astro Project
Traduzione:
Installazione manuale
Traduzione:
Package.json Scripts
Traduzione:
Project Creation
Template disponibili
Traduzione:
Comandi di sviluppo
Traduzione:
Costruire comandi
Traduzione:
Struttura del progetto
Struttura di base
Traduzione:
Struttura avanzata
Traduzione:
Configurazione
Traduzione:
Componenti
Astro Components
Traduzione:
Component with Slots
Traduzione:
Framework Components
// src/components/ReactCounter.jsx
import { useState } from 'react' ;
export default function ReactCounter () {
const [ count , setCount ] = useState ( 0 );
return (
< div >
< p > Count : { count }</ p >
< button onClick = {() => setCount ( count + 1 )}>
Increment
</ button >
< button onClick = {() => setCount ( count - 1 )}>
Decrement
</ button >
</ div >
);
}
Component Scripts
Traduzione:
Pages and Routing
Routing basato su file
Traduzione:
Pagina di base
Traduzione:
Dynamic Pages
Traduzione:
Catch-all Routes
Traduzione:
Layouts
Telaio di base
Traduzione:
Blog Layout
Traduzione:
Styling
Global Styles
Traduzione:
Component Styles
Traduzione:
Tailwind CSS Integrazione
Traduzione:
---
// src/components/Card.astro with Tailwind
export interface Props {
title: string;
description: string;
image?: string;
}
const { title, description, image } = Astro.props;
---
<div class="bg-white rounded-lg shadow-md overflow-hidden hover:shadow-lg transition-shadow duration-300">
{image && (
<img
src={image}
alt={title}
class="w-full h-48 object-cover"
/>
)}
<div class="p-6">
<h3 class="text-xl font-semibold text-gray-900 mb-2">{title}</h3>
<p class="text-gray-600 leading-relaxed">{description}</p>
<div class="mt-4">
<a
href="#"
class="inline-flex items-center text-blue-600 hover:text-blue-800 font-medium"
>
Read more
<svg class="ml-1 w-4 h-4" fill="currentColor" viewBox="0 0 20 20">
<path fill-rule="evenodd" d="M10.293 3.293a1 1 0 011.414 0l6 6a1 1 0 010 1.414l-6 6a1 1 0 01-1.414-1.414L14.586 11H3a1 1 0 110-2h11.586l-4.293-4.293a1 1 0 010-1.414z" clip-rule="evenodd"></path>
</svg>
</a>
</div>
</div>
</div>
Content Collections
Configurazione
Traduzione:
Traduzione:
npm creare astro@latest
cd myproject
npm install
npm run to
Traduzione:
const saluto = "Ciao, Astro!";
Traduzione:
Traduzione:
Benvenuti al futuro dello sviluppo web.
Traduzione:
__HTML_TAG_83_
.gregazione {
testo-align: centro;
imbottitura: 2rem;
__HTML_TAG_84_
Traduzione:
Using Collections
Traduzione:
Integrazioni
Integrazione reattiva
Traduzione:
// src/components/ReactCounter.jsx
import { useState } from 'react' ;
export default function ReactCounter ({ initialCount = 0 }) {
const [ count , setCount ] = useState ( initialCount );
return (
< div className = "counter" >
< h3 > React Counter </ h3 >
< p > Count : { count }</ p >
< div className = "counter-buttons" >
< button onClick = {() => setCount ( count - 1 )}> - </ button >
< button onClick = {() => setCount ( count + 1 )}> + </ button >
< button onClick = {() => setCount ( 0 )}> Reset </ button >
</ div >
</ div >
);
}
---
// Using React component in Astro
import Layout from '../layouts/Layout.astro';
import ReactCounter from '../components/ReactCounter.jsx';
---
<Layout title="Interactive Demo">
<main>
<h1>Interactive Components</h1>
<!-- Hydrate on page load -->
<ReactCounter client:load initialCount={5} />
<!-- Hydrate when visible -->
<ReactCounter client:visible initialCount={10} />
<!-- Hydrate on idle -->
<ReactCounter client:idle />
<!-- Hydrate on media query -->
<ReactCounter client:media="(max-width: 768px)" />
</main>
</Layout>
Vue Integration
Traduzione:
<!-- src/components/VueCounter.vue -->
<template>
<div class="counter">
<h3>Vue Counter</h3>
<p>Count: {{ count }}</p>
<div class="counter-buttons">
<button @click="decrement">-</button>
<button @click="increment">+</button>
<button @click="reset">Reset</button>
</div>
</div>
</template>
<script setup>
import { ref } from 'vue';
const props = defineProps({
initialCount: {
type: Number,
default: 0
}
});
const count = ref(props.initialCount);
const increment = () => count.value++;
const decrement = () => count.value--;
const reset = () => count.value = 0;
</script>
<style scoped>
.counter {
border: 1px solid #e5e7eb;
border-radius: 8px;
padding: 1rem;
margin: 1rem 0;
}
.counter-buttons {
display: flex;
gap: 0.5rem;
margin-top: 1rem;
}
button {
padding: 0.5rem 1rem;
border: 1px solid #d1d5db;
border-radius: 4px;
background: white;
cursor: pointer;
}
button:hover {
background: #f9fafb;
}
</style>
MDX Integrazione
Traduzione:
Traduzione:
funzione saluto(nome) {
ritorno Hello, ${name}!;
Traduzione:
Islands Architecture
Direttive dei clienti
Traduzione:
Ottimizzazione delle prestazioni
Traduzione:
API Routes
Basic API Routes
Traduzione:
Routes API dinamiche
Traduzione:
Traduzione:
Ottimizzazione immagine
Astro Assets
Traduzione:
# Image Processing
Traduzione:
Distribuzione statica
Traduzione:
Vercel Deployment
Traduzione:
Netlify Deployment
Traduzione:
Docker Deployment
Traduzione:
Rendering Server-Side
Traduzione:
Analisi Bundle
Traduzione:
Tecniche di ottimizzazione
Traduzione:
Testing
Unit Testing
Traduzione:
// tests/components/Card.test.ts
import { experimental_AstroContainer as AstroContainer } from 'astro/container' ;
import { expect , test } from 'vitest' ;
import Card from '../src/components/Card.astro' ;
test ( 'Card component renders correctly' , async () => {
const container = await AstroContainer . create ();
const result = await container . renderToString ( Card , {
props : {
title : 'Test Card' ,
description : 'Test description'
}
});
expect ( result ). toContain ( 'Test Card' );
expect ( result ). toContain ( 'Test description' );
});
E2E Testing
// tests/e2e/homepage.spec.ts
import { test , expect } from '@playwright/test' ;
test ( 'homepage loads correctly' , async ({ page }) => {
await page . goto ( '/' );
await expect ( page ). toHaveTitle ( /My Astro Site/ );
await expect ( page . getByRole ( 'heading' , { name : 'Welcome' })). toBeVisible ();
});
test ( 'navigation works' , async ({ page }) => {
await page . goto ( '/' );
await page . getByRole ( 'link' , { name : 'About' }). click ();
await expect ( page ). toHaveURL ( '/about' );
});
Risoluzione dei problemi
Questioni comuni
Costruire errori
Traduzione:
Idration Issues
Traduzione:
Emissione delle prestazioni
Traduzione:
Migliori Pratiche
Project Organization
Traduzione:
Traduzione:
SEO # Migliori Pratiche
Traduzione:
Sommario
Astro è un potente generatore di sito statico che eccelle nella costruzione di siti web con prestazioni eccezionali. Le caratteristiche principali includono:
Islands Architecture : Nave JavaScript minimo con idratazione selettiva
Framework Agnostic : Utilizzare React, Vue, Svelte o qualsiasi quadro
Content Collections : Gestione dei contenuti di tipo sicuro con validazione
** Routing basato su file**: Intuitive routing basato sulla struttura dei file
Image Optimization : elaborazione e ottimizzazione delle immagini integrate
Performance First : Zero JavaScript per impostazione predefinita con opt-in interattività
Per ottenere risultati ottimali, sfruttare la generazione statica per le pagine dei contenuti, utilizzare le direttive del cliente in modo giudiziario per l'interattività, ottimizzare le immagini con gli strumenti incorporati di Astro e seguire i modelli di raccolta dei contenuti per la gestione dei contenuti scalabile.
__HTML_TAG_85_
copia funzioneToClipboard() {}
const commands = document.querySelectorAll('code');
tutti i Comandi = '';
comandi. per ogni(cmd => AllCommands += cmd.textContent + '\n');
navigatore.clipboard.writeText(tutti iComandi);
alert('Tutti i comandi copiati a clipboard!');
funzione generaPDF() {
finestra.print();