Firstly, spontaneous abortion is simply the medical word for miscarriage. It occurs when the mother's body initiates labor before the foetus/baby is mature enough to survive outside the womb. This could occur because the foetus has already died in the womb for some reason, or because there is a foetal abnormality (ie severe chromosomal abnormalities such as some types of down syndrome will result in early miscarriage) or the mother gets really sick. If you are addicted to heroin and you stop while you are pregnant, this creates a huge physical stress on the body which is why 'going cold turkey' is dangerous - this can definitely trigger spontaneous abortion.
Basically, hormonal signals, nutrient exchange, and inflammatory signals all need to be normal to maintain pregnancy. If they are not, the mother's body can trigger a spontaneous abortion.
So if you are injecting heroin, you run the risk of introducing infection which could be passed to the foetus and cause harm or death and subsequent abortion. Likewise if you od, the baby's oxygen supply could be temporarily cut off leading to foetal injury and abortion.
If you are addicted and just using levels to maintain your addiction, your foetus is addicted too. So if you had clean heroin injecting with clean needles at a dose that just keeps you 'normal' this won't cause spontaneous abortion. The baby doesn't breathe til it's born so respiratory depression isn't a problem. It gets its oxygen from your blood. However, injecting heroin (or anything) there is always the risk of introducing infection, no matter how clean you are being. Unless you are iv ing hospital morphine in a sterile vial, it is not sterile. So changing to methadone takes away the risk of infection, and also reduces the risk of overdosing, since you know what dose you are getting.
Of course your baby will have to withdraw when it is born......but of all drugs opiates are actually quite safe in pregnancy. They don't cause birth defects or brain injury like other drugs such as cigarettes and alcohol and cocaine. Codeine is often prescribed to pregnant women for pain.
Hope this helps!