To effectively alleviate the aggregation of catalytic active sites, the rational design and simple preparation of a nanocatalyst with an ideal configuration plays a decisive role. Herein, a CoFe-prussian blue analogue (PBA) decorated with composite carbon/nitrogen sources (C/Ns) is prepared as the precursor to synthesize a three-dimensional (3D) ternary nanocomposite by a two-step calcination method. In this 3D nanocomposite, the 0D CoFe alloy nanoparticles derived from the PBA are mostly embedded in 1D N-doped carbon nanotubes (NCNTs) and partially loaded on 2D N-doped carbon nanosheets. Accordingly, the aggregation of alloy nanoparticles is greatly alleviated while a 3D nanostructure with large specific surface area, porosity, good electron transport properties and exposed active sites can be formed. The alloy nanoparticles are excellent electrocatalysts for oxygen reduction/oxygen evolution reactions, while also facilitating the formation of conductive NCNTs. Density Functional Theory calculations confirm the synergy between the Fe, N doped carbon and (110) crystal plane of the CoFe alloy that accounts for the electrocatalytic activity. The assembled Zn-air batteries show an open circuit voltage of 1.510 V, power density of 196.1 mW cm−2 and an operational durability of 300 h. Moreover, the assembled all-solid-state batteries display the potential for practical applications. This work demonstrates that the configuration of the electrocatalyst can significantly affect its catalytic performance.